DEV Community

Stylus07
Stylus07

Posted on

2 1

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


Brute Force Approach
Time Complexity : O(n^2)
Space Complexity : O(1)

Sort Approach
Time Complexity : O(nlogn)
Space Complexity : O(1)

Optimized Approach via hash

var containsDuplicate = function (nums) {
    let hashMap = {};
    for (let num of nums) {
        if (num in hashMap) {
            return true;
        }
        hashMap[num] = 1;
    }
    return false;
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

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

Okay