DEV Community

codingpineapple
codingpineapple

Posted on

3 1

LeetCode 217. Contains Duplicate (javascript solution)

Description:

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:

Time Complexity : O(n)
Space Complexity: O(n)

var containsDuplicate = function(nums) {
    const map = {}
    for(const num of nums) {
        // If we have seen this num before return true
        if(map[num]) return true
        map[num] = true
    }
    return false
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay