Hashing for lookup
Hashing for lookup means using a HashMap or HashSet so a value you have seen can be found in expected O(1) time. It turns nested scans into one pass plus a table.
Detailed Explanation
The brute force for "have I seen this before?" is another loop. That is O(n²). A hash table stores what you have seen. Each check is average O(1). Two-sum on an unsorted array is the poster case: as you walk, ask whether target - xs[i] is already in the map; if not, record xs[i] and its index.
The same pattern appears as a set of visited nodes in a graph, a frequency map for anagrams, a map of prefix sums to counts, and a dictionary of character indexes for a sliding window. The structure is the algorithm: you are trading memory for time.
Correctness depends on equals and hashCode. For integers and strings the JDK types are fine. For arrays you must not use them as keys (identity). For custom types you must override both methods. Worst-case hashing is O(n) per lookup if everything collides; interview analysis still says expected O(1), and Java 8+ treeifies colliding bins.
You reach for a hash table when membership or last-seen index is the question and order of keys does not matter. You reach for a sort plus two pointers when you may sort and you want O(1) extra memory. You reach for a tree map when you need the next larger key.
The cost is memory: a map of n integers is much larger than the array. The cost of storing the first index vs the last index is a different answer to "which pair". Decide before you write put. The cost of mutating a key is a lost entry. The cost of using contains plus get is two lookups; get and a null check (or getOrDefault) is one.
Integer boxing happens on every HashMap<Integer, Integer> put. For hot numeric sets, a BitSet or a primitive map library can matter. In interview Java, HashMap is the expected tool.
Code Examples
import java.util.HashMap;
import java.util.Map;
public class TwoSum {
static int[] indexes(int[] xs, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < xs.length; i++) {
Integer j = seen.get(target - xs[i]);
if (j != null) {
return new int[] {j, i};
}
seen.put(xs[i], i);
}
return new int[0];
}
public static void main(String[] args) {
int[] ans = indexes(new int[] {2, 7, 11, 15}, 9);
System.out.println(ans[0] + "," + ans[1]); // prints: 0,1
}
}import java.util.HashSet;
import java.util.Set;
public class FirstRepeat {
static Integer firstRepeat(int[] xs) {
Set<Integer> seen = new HashSet<>();
for (int x : xs) {
if (!seen.add(x)) {
return x;
}
}
return null;
}
public static void main(String[] args) {
System.out.println(firstRepeat(new int[] {1, 2, 3, 2, 1})); // prints: 2
}
}Common Gotchas
Decide first vs last index before you `put`
putting first overwrites the index you needed for "earliest".
Arrays as keys use identity
wrap them or use a string/list key with real `equals`.
Expected O(1) is not a worst-case guarantee
pathological hashes degrade; still the right interview tool.
`get` plus `put` on `Integer` boxes
not wrong, but it is not free on huge inputs.