Leetcode Note: Go - Count Number of Pairs With Absolute Difference K

Count Number of Pairs With Absolute Difference K - LeetCode
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/

  • Go 言語で取り組んだメモ

回答

Golang map solution with explanation - Count Number of Pairs With Absolute Difference K - LeetCode
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/solutions/1478907/golang-map-solution-with-explanation/

func countKDifference(nums []int, k int) int {
    res := 0
    m := make(map[int] int)
    
    for _, num := range nums {
        m[num]++
        res += m[num + k] + m[num - k]
    }
    
    return res
}