Leetcode Note: Go - Number of Good Pairs
Number of Good Pairs - LeetCode
https://leetcode.com/problems/number-of-good-pairs/
- Go 言語で取り組んだメモ
回答
Golang O(n) Solution - Number of Good Pairs - LeetCode
https://leetcode.com/problems/number-of-good-pairs/solutions/1023592/golang-o-n-solution/
func numIdenticalPairs(nums []int) int {
cnt := make(map[int]int)
var pairs int
for _, num := range nums {
pairs += cnt[num] //if num not in hash map "cnt", map returns default value of int (ie 0)
cnt[num]++
}
return pairs
}