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

Example 1:

Input: nums = [1,2,3,1]
Output: true

用Object存取顯示過的數字

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function(nums) {
    let tmp = {};
    for(let i = 0 ; i < nums.length ; i++){

        if(!tmp[nums[i]]){
            tmp[nums[i]] = 1
        }else{
            return true
        }
    }
    return false
};

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。