The Number of Good Pairs problem asks you to count the number of pairs (i, j) where i < j and arr[i] == arr[j]. This Number of Good Pairs coding problem has a clean O(n) solution using frequency counting and the combination formula.
Apple, Microsoft, Meta, Amazon, Google, Bloomberg, and many others ask this as a quick screening problem that verifies hash map and combination counting skills. The array, math, hash table, and counting interview pattern is demonstrated at its most fundamental level.
Frequency count + running combination. For each element value, track its frequency so far. When element x is seen for the f-th time, it can pair with all f-1 previous occurrences → add f-1 to the result. This gives the count incrementally without a separate combination formula step.
Array: [1,2,3,1,1,3].
Good Pairs is the canonical "count pairs with equal values" problem. The incremental counting trick — add (current_freq - 1) when each element is seen — is O(n) and avoids a second pass. This same technique applies to "count pairs with sum k" (use complement hash map) and other pair-counting problems. Memorize the pattern: process elements left to right, for each element query the map for how many valid partners already exist, then update the map.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find The Least Frequent Digit | Easy | Solve | |
| Count Number of Bad Pairs | Medium | Solve | |
| Count Nice Pairs in an Array | Medium | Solve | |
| Count Number of Distinct Integers After Reverse Operations | Medium | Solve | |
| Sum of Digit Differences of All Pairs | Medium | Solve |