Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+5 votes

I want to check if the value is in a set without looping through it to check if the value is present in the set to improve efficiency. Is that possible?

asked in CSC371 by (1 point)
+1

You should use HashSet as it will use hashing, using a list won't be useful because you probably don't want repeating elements is you are using a set. I think if you want repeating elements then you should use a different DS.

1 Answer

+2 votes

With a HashSet, the contains(obj) will check if obj is in the set in O(1) time.
With a TreeSet, it'll be O(log n) time.

However, if you use a List, then contains() will take O(N) time.

answered by (3.5k points)
...