Find pairs and duplicates in one pass by remembering what you have seen.
Before position j is read, the map holds every earlier value with its first position, so a partner found in the map always sits at an earlier position.
Use it when you must test "has this value appeared before?", or find two values that satisfy an exact relation, such as a total or a difference. Signal words: appeared before, first repeat, two values that add up to, report the positions, one pass.
Start: an empty hash set, or a hash map from value to position.
Step: compute what the current value needs (itself, or target - value). Look it up. If it is there, the answer is complete. Otherwise store the current value.
Stop: at the first match, or when the values run out.
It does not find the closest value, or values inside a range. The lookup answers only "is this exact value here?".
Cost: O(n) time on average and O(n) space. The check of every pair costs O(n^2) time and O(1) space.
def first_repeat(values):
# [1] An empty set of values seen so far.
seen = set()
for i in range(len(values)):
# [2] A value already in the set has appeared before: this is the first repeat.
if values[i] in seen:
return i
# [3] A new value joins the set.
seen.add(values[i])
# [4] No value appeared twice.
return -1public int firstRepeat(int[] values) {
// [1] An empty set of values seen so far.
java.util.HashSet<Integer> seen = new java.util.HashSet<>();
for (int i = 0; i < values.length; i++) {
// [2] A value already in the set has appeared before: this is the first repeat.
if (seen.contains(values[i])) {
return i;
}
// [3] A new value joins the set.
seen.add(values[i]);
}
// [4] No value appeared twice.
return -1;
}int firstRepeat(vector<int>& values) {
// [1] An empty set of values seen so far.
unordered_set<int> seen;
for (int i = 0; i < (int)values.size(); i++) {
// [2] A value already in the set has appeared before: this is the first repeat.
if (seen.count(values[i])) {
return i;
}
// [3] A new value joins the set.
seen.insert(values[i]);
}
// [4] No value appeared twice.
return -1;
}function firstRepeat(values) {
// [1] An empty set of values seen so far.
const seen = new Set();
for (let i = 0; i < values.length; i++) {
// [2] A value already in the set has appeared before: this is the first repeat.
if (seen.has(values[i])) {
return i;
}
// [3] A new value joins the set.
seen.add(values[i]);
}
// [4] No value appeared twice.
return -1;
}def pair_with_total(values, target):
# [1] A map from each value seen so far to its first position.
first_at = {}
for j in range(len(values)):
# [2] The partner that completes the total.
need = target - values[j]
# [3] If the partner appeared earlier, report both positions.
if need in first_at:
return [first_at[need], j]
# [4] Remember this value, unless an earlier copy is already stored.
if values[j] not in first_at:
first_at[values[j]] = j
# [5] No pair makes the total.
return [-1, -1]public int[] pairWithTotal(int[] values, int target) {
// [1] A map from each value seen so far to its first position.
java.util.HashMap<Integer, Integer> firstAt = new java.util.HashMap<>();
for (int j = 0; j < values.length; j++) {
// [2] The partner that completes the total.
int need = target - values[j];
// [3] If the partner appeared earlier, report both positions.
if (firstAt.containsKey(need)) {
return new int[] {firstAt.get(need), j};
}
// [4] Remember this value, unless an earlier copy is already stored.
if (!firstAt.containsKey(values[j])) {
firstAt.put(values[j], j);
}
}
// [5] No pair makes the total.
return new int[] {-1, -1};
}vector<int> pairWithTotal(vector<int>& values, int target) {
// [1] A map from each value seen so far to its first position.
unordered_map<int, int> firstAt;
for (int j = 0; j < (int)values.size(); j++) {
// [2] The partner that completes the total.
int need = target - values[j];
// [3] If the partner appeared earlier, report both positions.
if (firstAt.count(need)) {
return {firstAt[need], j};
}
// [4] Remember this value, unless an earlier copy is already stored.
if (!firstAt.count(values[j])) {
firstAt[values[j]] = j;
}
}
// [5] No pair makes the total.
return {-1, -1};
}function pairWithTotal(values, target) {
// [1] A map from each value seen so far to its first position.
const firstAt = new Map();
for (let j = 0; j < values.length; j++) {
// [2] The partner that completes the total.
const need = target - values[j];
// [3] If the partner appeared earlier, report both positions.
if (firstAt.has(need)) {
return [firstAt.get(need), j];
}
// [4] Remember this value, unless an earlier copy is already stored.
if (!firstAt.has(values[j])) {
firstAt.set(values[j], j);
}
}
// [5] No pair makes the total.
return [-1, -1];
}This version stores the value in the map first, and then looks up its complement.
This version stores every value at its latest position, with first_at[value] = j and no check.
A tempting fix is to sort a copy of the amounts and use two pointers, as in Module 4. The pair is found, but the positions belong to the sorted list.
Your team lead asks for the two payments whose total is closest to 100, or the pairs whose total is at most 100. The complement is one exact value, and here no single value is the answer.
systemdesignmastery.dev/dsa/cheatsheets/seen-set-and-complement