Distinct Integer Pairs
intro
Implement int distinct_pairs(const vector<pair<int,int>>& pts) (pts is a list of (int, int) tuples in Python, an array of 2-element arrays in JavaScript) that returns the number of distinct pairs in pts, treating (a, b) and (b, a) as different pairs when a != b. Insert every pair into a set<pair<int,int>> (in Python, a set of tuples, since tuples are hashable) to eliminate duplicates, then return the resulting set's size.
In JavaScript a Set of arrays will not work: Set keys objects by reference identity, so two separately-created [1, 2] arrays are distinct entries and nothing ever deduplicates; a naive port silently returns pts.length. Serialize each pair into a string key (e.g. `${a},${b}`) instead; the separator is load-bearing, since `${a}${b}` would merge (1, 23) with (12, 3).
Example: distinct_pairs({{1, 2}, {2, 1}, {1, 2}, {3, 3}}) returns 3. {1, 2} and {2, 1} are kept as two separate pairs (order matters), the second {1, 2} is a duplicate that collapses into the first, and {3, 3} is its own distinct pair, leaving {1,2}, {2,1}, {3,3}.
Input format: each test in tests/*.in is laid out as:
- Line 1:
n, the number of pairs. - Next
nlines:x y, one ordered pair per line.
Test 03.in reads:
3
1 2
2 1
1 2
This is three pairs where (1, 2) appears twice and (2, 1) once; order matters, so there are 2 distinct pairs.
Where you'll use it:
- 36. Valid Sudoku (dedupe seen (value, unit) pairs in a set)
- 2352. Equal Row and Column Pairs
✦ Solution & editorial unlock with the pass.