Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Using HashMap and Binomial Coefficient with k = 2

Ejike Nwude

Dec 7, 2023

class Solution {   public int numGoodPairs(int[] nums) {     int pairCount = 0;     Map<Integer, Integer> frequency = new HashMap<>();     for (Integer num : nums) {       frequency.put(num, frequency.getOrDefault(num, 0) + 1);     }     for (Map.Entry<Integer, Integer> entry : frequency.entrySet()) {       int value = entry.getValue();       pairCount += value * (value - 1) / 2;     }     return pairCount;   } }

3

0

Comments
Comments

On this page