Hash maps for lookup and counting
A hash map remembers what you have already seen, so a single scan can find duplicates, pairs and running counts.
The idea
Does any value in nums repeat? Comparing every pair of 6 values takes 15 comparisons.
n values form n(n - 1)/2 pairs. For 10,000 values that is about 50 million comparisons.
Intuition
At index 4 the question is: did a 1 come earlier? Looking back rereads 4 cells.
A set named seen already holds 5, 1, 8 and 3, the values read so far. Asking it about 1 checks one bucket.
A hash set sends 1 to bucket 1 mod 7 = 1. The question costs about one step, however many values came before.
Complexity
One scan with a set costs O(n) time and O(n) space. Comparing every pair costs O(n²) time.
Each set or map question costs O(1) on average. The memory holds up to one entry per value read.
Pitfalls
Ask the set before adding x. Adding first makes 4 find itself and report a repeat that is not there.
In JavaScript and Java, -2 % 5 is -2. Add k and take % k again, so the key is 3.
Sums -2 and 3 differ by 5, so they must share a key. Python's % already returns 3.
Variations
A map can store a partner, not only a count. egg and add match because e → a and g → d never change.
Code
Pseudocode
seen ← empty set
for each x in nums
if x in seen: return true
add x to seen
return false
Python
def contains_duplicate(nums):
seen = set()
for x in nums:
if x in seen:
return True
seen.add(x)
return False
JavaScript
function containsDuplicate(nums) {
const seen = new Set();
for (const x of nums) {
if (seen.has(x)) return true;
seen.add(x);
}
return false;
}
Java
boolean containsDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int x : nums) {
if (seen.contains(x)) return true;
seen.add(x);
}
return false;
}
Practice problems (6)
Contains duplicate Warm-up
Given an integer array nums, return true if some value appears at least twice, and false if every value is distinct.
Scan the array once and keep a set of the values read so far.
Fill the seen-set Warm-up
Follow a seen-set scan of [7, 3, 10, 4, 3]. The set has 7 buckets and sends a value to bucket value mod 7. Answer each question before the step plays.
Isomorphic strings Standard
Two strings s and t are isomorphic when you can replace the letters of s to get t. Every copy of a letter must become the same letter, and two different letters may not become the same letter. A letter may stay as it is.
Return true if s and t are isomorphic.
Subarray sums divisible by k Standard
Given an integer array nums and an integer k, return how many non-empty subarrays have a sum divisible by k. A subarray is a run of neighbouring cells.
Two prefix sums with the same remainder mod k differ by a multiple of k, so the cells between them form such a subarray.
Cost of counting distinct values Standard
Pick the average time and space complexity of countDistinct for an array of length n.
Longest consecutive sequence Challenge
Given an unsorted integer array nums, return the length of the longest run of consecutive integers, such as -1, 0, 1, 2. The values of a run can sit at any positions.
Solve it in O(n) time, so sorting is not allowed.
Next on the path: Overlapping subproblems and memoization