02 · Arrays & Strings in Java

#1512 Number of Good Pairs

Easysolve on LeetCode ↗

frequently reported atBloombergGoogleMicrosoft

the problem

In plain words

A pair of indexes (i, j) is good when i < j and nums[i] == nums[j]. Return how many good pairs the array has.

in nums = [1,2,3,1,1,3]

out 4

(0,3), (0,4), (3,4), (2,5).

in nums = [1,1,1,1]

out 6

Every pair is good: 4·3/2.

in nums = [1,2,3]

out 0

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #1
      Two Sum
      Easyin Hashing
    2. #560
      Subarray Sum Equals K
      Mediumin Hashing
    3. #217
      Contains Duplicate
      Easyin Hashing