Problem Description

gives you an integer array nums. Returns true if any value appears in the array at least twice; returns false if each element in the array is different.

Example 1:

 输入:nums = [1,2,3,1]
输出:true

Example 2:

 输入:nums = [1,2,3,4]
输出:false

Example 3:

 输入:nums = [1,1,1,3,3,4,3,2,4,2]
输出:true
Likou original title address: https://leetcode.cn/problems/contains-duplicate

solution

Scenario 1 Traverse the array and use the object to record whether the element has appeared before

Idea: Initially, define an object for recording. When we traverse the array, we can get each item in the array to see if this item has appeared in the object before. If it has appeared, it means that it is repeated with the previous one; if it has not appeared, it means that it is new. item, put the new item into the object, and wait for the subsequent matching to see if it will be duplicated with the current one. code show as below:

 var containsDuplicate = function (nums) {
    let obj = {} // 1. 定义对象用于存储记录每一项是否出现过
    for (let i = 0; i < nums.length; i++) { // 2. 遍历数组进行一项一项的比较
        let key = nums[i] // 3. 拿到每一项的值
        // 4. 看看此项是否在对象中。对象中是否有某个属性名key  key in obj ?
        if (key in obj) { // 7. 未来的某一时刻,突然出现了,当前项之前有过
            return true // 8. 就说明有重复项,返回告知结果即可
        } else { // 5. 一开始肯定是没有的,所以咱就给对象记录一下
            obj[key] = 1 // 6. 给对象加上一个key、value属性名属性值
        }
    }
    return false
};

leetcode submission result graph

Scenario 1 Traverse the array and use the Map collection to record whether the element has appeared before

The idea and the object are one, just replace the use of the object record with the Map collection record.

 var containsDuplicate = function (nums) {
    let map = new Map() // 1. 定义一个集合用来记录
    for (let i = 0; i < nums.length; i++) { // 2. 遍历数组,拿到每一项
        // 3. 一开始集合是空的,所以看else
        if (map.has(nums[i])) { // 5. 若后续项在集合中曾经有过(即曾经追加,现在又来个一样的)
            return true // 6. 就说明重复啦,返回告知结果即可
        } else { // 4. 给集合追加当前项,存一份用于后续的判断
            map.set(nums[i], 1) 
        }
    }
    return false // 7. 要是操作了一波以后,依然是没法返回true,说明每一项都不一样,即没有重复项
};

leetcode submission result graph

Option 3 Compare whether the length of the array changes before and after deduplication

Idea: Assuming that the array has duplicate elements, after deduplication, the number of elements in the array will be less, then the length of the array after deduplication will be smaller than the length of the array before deduplication. Of course, if the length of the array before and after deduplication is the same, it means that there are no duplicates. Therefore, write the code as follows:

 var containsDuplicate = function (nums) {
    let originLength = nums.length // 1. 获取原始数组长度
    let removeDuplicateLength = [...new Set(nums)].length // 2. 获取去重以后的数组长度
    // 3. 看看两个数组长度是否相等
    if (originLength == removeDuplicateLength) { // 4. 若相等没变化,说明没有重复项
        return false
    } else {
        return true // 5. 若不等,就说明有重复项被去除掉了
    }
};

leetcode submission result graph

Summarize

Comparing the above three solutions, we find that the map set is indeed the most efficient. Therefore, when both objects and Map collections can be used, priority should be given to the use of Map collections


水冗水孚
1.1k 声望585 粉丝

每一个不曾起舞的日子,都是对生命的辜负