1320. 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.

您在真實的面試中是否遇到過這個題?  

樣例

Given nums = [1,1], return ture.

無難度題目,用關聯容器即可

class Solution {
public:
    /**
     * @param nums: the given array
     * @return: if any value appears at least twice in the array
     */
    bool containsDuplicate(vector<int> &nums) {
        // Write your code here
        unordered_map<int,int> mmap;
        for(int i=0;i<nums.size();i++){
            mmap[nums[i]]++;
            if(mmap[nums[i]]>=2) return true;
        }
        return false;
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章