[Looking for comment]Golang O(n) space solution


  • 0
    S

    Create an array for answer and a map for storing the computing result.

    func findDuplicates(nums []int) []int {
        intDict := make(map[int]int, len(nums))
        var ansArr []int
    
        for _, val := range nums {
            if _, ok := intDict[val]; ! ok {
                intDict[val] = 0
                continue
            }
            intDict[val] = intDict[val] + 1
        }
        
        for index, val := range intDict {
            if val > 0 {
                ansArr = append(ansArr, index)
            }
        }
        return ansArr
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.