美文网首页LeetCode By Go
[LeetCode By Go 48]217. Contains

[LeetCode By Go 48]217. Contains

作者: miltonsun | 来源:发表于2017-08-22 17:45 被阅读6次

题目

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

解题思路

nums中字符放进map中,值为字符出现的次数,次数超过1即为重复

代码

func containsDuplicate(nums []int) bool {
    if len(nums) < 2 {
        return false
    }
    
    var numMap map[int]int
    numMap = make(map[int]int)

    for _, v := range nums {
        numMap[v]++

        if numMap[v] > 1 {
            return true
        }
    }

    return false
}

相关文章

网友评论

    本文标题:[LeetCode By Go 48]217. Contains

    本文链接:https://www.haomeiwen.com/subject/adwldxtx.html