[LeetCode | Javascript] Contains Duplicate

박기영·2023년 1월 7일

LeetCode

목록 보기
2/41

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.

solution

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function(nums) {
    const set = new Set(nums);

    if(set.size !== nums.length){
        return true;
    }

    return false;
};

매우 쉬운 문제다.
입력된 배열 내에 최소 두 번, 중복 등장하는 원소가 있다면 true, 아니면 false를 반환한다.
Set 객체를 활용하여 중복 제거를 했고, 제거 전과 후 길이 변화를 이용하여 이를 확인한다.

profile
나를 믿는 사람들을, 실망시키지 않도록

0개의 댓글