Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an array of integers nums, return the number of good pairs.
A pair (i, j) is called good if nums[i] == nums[j] and i < j.
Example 1:
Input: nums = [1,2,3,1,1,3] Output: 4 Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
Example 2:
Input: nums = [1,1,1,1] Output: 6 Explanation: Each pair in the array are good.
Example 3:
Input: nums = [1,2,3] Output: 0
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100Problem summary: Given an array of integers nums, return the number of good pairs. A pair (i, j) is called good if nums[i] == nums[j] and i < j.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[1,2,3,1,1,3]
[1,1,1,1]
[1,2,3]
number-of-pairs-of-interchangeable-rectangles)substrings-that-begin-and-end-with-the-same-letter)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1512: Number of Good Pairs
class Solution {
public int numIdenticalPairs(int[] nums) {
int ans = 0;
int[] cnt = new int[101];
for (int x : nums) {
ans += cnt[x]++;
}
return ans;
}
}
// Accepted solution for LeetCode #1512: Number of Good Pairs
func numIdenticalPairs(nums []int) (ans int) {
cnt := [101]int{}
for _, x := range nums {
ans += cnt[x]
cnt[x]++
}
return
}
# Accepted solution for LeetCode #1512: Number of Good Pairs
class Solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
ans = 0
cnt = Counter()
for x in nums:
ans += cnt[x]
cnt[x] += 1
return ans
// Accepted solution for LeetCode #1512: Number of Good Pairs
impl Solution {
pub fn num_identical_pairs(nums: Vec<i32>) -> i32 {
let mut ans = 0;
let mut cnt = [0; 101];
for &x in nums.iter() {
ans += cnt[x as usize];
cnt[x as usize] += 1;
}
ans
}
}
// Accepted solution for LeetCode #1512: Number of Good Pairs
function numIdenticalPairs(nums: number[]): number {
const cnt: number[] = Array(101).fill(0);
let ans = 0;
for (const x of nums) {
ans += cnt[x]++;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.