Created
July 18, 2020 05:45
-
-
Save shixiaoyu/4b315aa0a58f9b2d648605f0a129b60c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int numIdenticalPairs(int[] nums) { | |
if (nums == null || nums.length == 0) { | |
return 0; | |
} | |
// key: int value; value: number of occurrence | |
Map<Integer, Integer> lookup = new HashMap<>(); | |
int goodPairsCount = 0; | |
for (int i : nums) { | |
if (lookup.containsKey(i)) { | |
goodPairsCount += lookup.get(i); | |
lookup.put(i, lookup.get(i) + 1); | |
} else { | |
lookup.put(i, 1); | |
} | |
} | |
return goodPairsCount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment