Leetcode Note: Go - Find the Difference of Two Arrays

Find the Difference of Two Arrays - LeetCode
https://leetcode.com/problems/find-the-difference-of-two-arrays/

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

回答

Find the Difference of Two Arrays - LeetCode
https://leetcode.com/problems/find-the-difference-of-two-arrays/solutions/3482237/golang-fastest-and-clean-solution-17ms-beats-100-by-runtime/

func setDifference(a, b []int) []int {
    d := [2001]bool{}
    ans := []int{}
    for _, x := range b {
        d[x + 1000] = true
    }
    for _, x := range a {
        if !d[x + 1000] {
            ans = append(ans, x)
            d[x + 1000] = true // prevent duplicates from being added
        }
    }
    return ans
}

func findDifference(nums1 []int, nums2 []int) [][]int {
    return [][]int{ setDifference(nums1, nums2), setDifference(nums2, nums1) }
}