HashSet uses a hash table to ensure unique elements.
HashSet utilizes a hash table under the hood, which means every time an element is added, it computes a hash code and checks for duplicates using the hash code. This makes both add and contains operations run in average constant time, O(1). For example, if you have a HashSet of product IDs, adding a duplicate ID will not change the set.