Skip to content

Instantly share code, notes, and snippets.

@shixiaoyu
Created July 18, 2020 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shixiaoyu/4b315aa0a58f9b2d648605f0a129b60c to your computer and use it in GitHub Desktop.
Save shixiaoyu/4b315aa0a58f9b2d648605f0a129b60c to your computer and use it in GitHub Desktop.
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