[LeetCode By Go 48]217. Contains Duplicate

题目

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
}
    原文作者:miltonsun
    原文地址: https://www.jianshu.com/p/593a32b12000
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞