Junior — Middle
What data structure provides fast search of elements based on equality comparison, performed in constant time?
sobes.tech AI
Answer from AI
For quick element search by equality in constant time, a data structure based on hashing — HashSet or HashMap in Java — is usually used.
HashSet stores unique elements and provides add, remove, and search operations in approximately O(1) on average, using a hash function to determine the position of an element.
Example of using HashSet:
import java.util.HashSet;
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
boolean containsApple = set.contains("apple"); // true
Thus, HashSet provides fast equality-based search thanks to hashing.