Find All Symmetric Pairs
Problem
You are given an array of n pairs of integers, pairs[i] = [a, b]. Two pairs [a, b] and [c, d] are symmetric if a == d and b == c. Return all symmetric pairs found in the array. Each symmetric relationship should be reported once, as the pair that appears first in the input. The first elements of all pairs are distinct.
Input / Output
- Input: 2-D array
pairsof shapen × 2. - Output: list of pairs
[a, b]such that[b, a]also exists later in the array.
Examples
Example 1
Input: pairs = [[11, 20], [30, 40], [5, 10], [40, 30], [10, 5]]
Output: [[30, 40], [5, 10]]
Explanation: [30,40] is symmetric with [40,30]; [5,10] with [10,5].
Example 2
Input: pairs = [[1, 2], [3, 4]]
Output: []
Example 3
Input: pairs = [[2, 2]]
Output: [[2, 2]] — a pair is symmetric with itself when a == b (some versions exclude this; state your assumption).
Constraints
1 <= n <= 10^5-10^9 <= a, b <= 10^9- All first elements are distinct.
Approach
The naive O(n²) scan compares every pair with every other. Because first elements are unique, a hash map from first element to second element lets us test symmetry in O(1):
- Iterate through the pairs in order.
- For the current pair
[a, b], check whether the map contains keybandmap[b] == a. If so,[b, a]appeared earlier, so report the earlier pair[b, a](or the current one — follow the required convention). - Insert
map[a] = b.
Checking before inserting guarantees each symmetric relationship is reported exactly once, and reporting on the second sighting preserves input order when the earlier pair is emitted.
If first elements are not distinct, store a set of second elements per key instead of a single value.
Complexity
- Time: O(n) average.
- Space: O(n) for the map.
Reference solution (Python)
def symmetric_pairs(pairs):
seen = {}
out = []
for a, b in pairs:
if b in seen and seen[b] == a:
out.append([b, a])
seen[a] = b
return out
Test cases
| Input | Expected |
|---|---|
[[11,20],[30,40],[5,10],[40,30],[10,5]] | [[30,40],[5,10]] |
[[1,2],[3,4]] | [] |
[[1,2],[2,1],[3,1]] | [[1,2]] |
[[7,7]] | [] (self-pair not counted in this implementation) |