Skip to content

Instantly share code, notes, and snippets.

@Ephraimiyanda
Created February 12, 2026 20:38
Show Gist options
  • Select an option

  • Save Ephraimiyanda/729b8a06cb2a2de771d0909be38c008d to your computer and use it in GitHub Desktop.

Select an option

Save Ephraimiyanda/729b8a06cb2a2de771d0909be38c008d to your computer and use it in GitHub Desktop.
Contains Duplicate

Question

Approach

As i loop through the numbers i check if a number exists onthemap.if it does i return true if it doesnt i and add the number to the map and continue. if there are no duplicates false is returned.

Complexity

  • Time complexity:O(N)

  • Space complexity:O(N)

Code

function containsDuplicate(nums: number[]): boolean {
    let map = new Map()
    for (let i = 0; i < nums.length; i++) {
        if (map.has(nums[i])) return  true
        map.set(nums[i], nums[i])

    }
    return false
};
scrnli_9WIcqHsivWUAZz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment