Number of Good Pairs

Problem Statement – 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

Logic

  • Initialize a counter
  • Loop over all the elements of nums array
  • if the given condition is satisfied then increment the counter
  • return result
class Solution {
    public int numIdenticalPairs(int[] nums) {
        int k=0;
        for(int i=0;i<nums.length-1;i++){
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]==nums[j]){
                    k++;
                }
            }
        }
        return k;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

WhatsApp Icon Join For Job Alerts