If you have two HashSet
s, comparing them by Set.equals
will be O(n) because only one set needs to be iterated through, and the other will be checked by contains
, which is itself O(1).
Note that for sets as small as yours the difference between O(n) and O(n2) is neglible, so even the naïvest approaches will yield good performance.