You develop a small contact manager for a client.
One important feature is the possibility to define a list of contacts.
At first, you started with an array,
but you realize quickly that you have to check for duplicates. You end in many places with something like:
Last time you were working with the list, you needed to send a campaign email to each contact:
You forgot to check for duplicates, you shipped it, and the campaign was sent twice to granny!
You don’t like that, and you’re right. Indeed, the code is fragile: you shouldn’t watch for the uniqueness constraint, it should be built in.
If you need a collection with uniqueness guaranteed, use a Set:
Of course, Array is a fine structure too – if duplicates are allowed or you need to access the nth element. In the end, you should work with classes properly representing your data: they will behave as you expect. In 99%, it’s more important than performance consideration, or you’ll end with fragile code.