Reputation: 27
I have a text file full of words. I want to add each of these words to a hashset. I also have a hashset of words I do not want.
Is it more efficient to:
Edit
There is far more words I want, than words I do not want.
Upvotes: 1
Views: 71
Reputation: 6780
The answer depends completely on the size of your lists. If you have 99999 words you don't want and 1 word you do, you should do option A. If you have 99999 words you want and 1 word you don't, you should do option B.
The reason behind this is obvious - option B gets more and more efficient the smaller the hash set of undesired words is since you have to check that entire set any time you insert a new word using option B.
From a purely theoretical view, both are the same in terms of worst case time complexity, but practically, there can be a big difference.
So basically, as with most solutions, the efficiency depends on how you expect your data to be structured.
Upvotes: 3