#44 Counting Identical Pairs in an Array

Companies:
Sportmaster
Themes:
AlgorithmsArraysPractice

An integer array nums is given. A pair (i, j) is called identical if nums[i] == nums[j] and i < j. Calculate the number of identical pairs by implementing the following method:

// Example:
// Given: nums = [4,1,2,4,4,2]
// Answer: 4
// Explanation: There are 4 identical pairs: (0,3), (0,4), (3,4), (2,5).

public class Main {
    public static void main(String[] args) {
        int[] nums = {4, 1, 2, 4, 4, 2};
        System.out.println(numberOfIdenticalPairs(nums));
    }

    public static int numberOfIdenticalPairs(int[] nums) {
        //TODO
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in