The easiest way, in my opinion, is to insert all the values โโinside the set and then check if its size is equal to its size . A set cannot contain duplicate values, so if any value is duplicated, it will not be inserted into the set.
This is also normal if you do not have millions of values, because the insertion into the set is done in O(logn) time, so the total validation time will be O(nlogn) .
If you want something optimal in complexity, you can do it O(n) times by going through the array and putting each value in the hash map , increasing its value: if the value doesn't exist in the set, you add it with count = 1. If it exists, you increase its number. Then you look at the hash map and verify that all values โโhave a counter .
alestanis
source share