Number of Good Pairs

Given an array of integers nums, return the number of good pairs. A pair (i, j) is good if nums[i] == nums[j] and i < j.

30:00

Number of Good Pairs
easy
Free written solution
Topics
Companies

Given an array of integers nums, return the number of good pairs. A pair (i, j) is good if nums[i] == nums[j] and i < j.

Example 1:
Input: {"nums":[1,2,3,1,1,3]}
Output: 4
Constraints:
  • 1≤nums.length≤1001 \leq \text{nums.length} \leq 100

  • 1≤nums[i]≤1001 \leq \text{nums}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,3,1,1,3]}

Keep one bucket per value. Every new element pairs with everything already in its bucket, so the bucket size is the number of new pairs.

nums

1

0

2

1

3

2

1

3

1

4

3

5

count buckets (earlier indices per value)

empty: no value seen yet

pairs

0

pairs (i, j) formed

none yet

Variables
VariableValue
pairs0
n6
DepthFunction Call
Stack empty
0/13