Contains Duplicate

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Solution in Go

func containsDuplicate(nums []int) bool {
    set := make(map[int]struct{})
    
    for _, n := range nums {
        _, contains := set[n]
        if contains {
            return true
        }
        
        set[n] = struct{}{}
    }
    
    return false
}
Subscribe via email

Get notified once/twice per month when new articles are published.

Copyright © 2022 - 2024 TheDeveloperCafe.
The Go gopher was designed by Renee French.