Skip to content

Instantly share code, notes, and snippets.

@shawn-dsz
Last active July 28, 2022 06:45
Show Gist options
  • Save shawn-dsz/668af3db16e5b41e97d3776aa0a994b8 to your computer and use it in GitHub Desktop.
Save shawn-dsz/668af3db16e5b41e97d3776aa0a994b8 to your computer and use it in GitHub Desktop.
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.
const expect = (actual, expected) => {
if (actual !== expected) {
console.log(`Expected ${actual} to equal ${expected}`);
}
};
var containsDuplicate = function (nums) {
var map = new Map();
for (const elm of nums) {
if (map.has(elm)) {
return true;
}
map.set(elm, true);
}
return false;
};
expect(containsDuplicate([1, 2, 3, 1]), true);
expect(containsDuplicate([1, 2, 3, 4]), false);
/*
https://leetcode.com/problems/contains-duplicate/
Example 1:
Input: nums = [1,2,3,1]
Output: true
Example 2:
Input: nums = [1,2,3,4]
Output: false
Example 3:
Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
Constraints:
1 <= nums.length <= 105
-109 <= nums[i] <= 109
*/
const expect = (actual, expected) => {
if (actual !== expected) {
console.log(`Expected ${actual} to equal ${expected}`);
}
};
var containsDuplicate = function (nums) {
var map = new Map();
for (const elm of nums) {
if (map.has(elm)) {
return true;
}
map.set(elm, true);
}
return false;
};
expect(containsDuplicate([1, 2, 3, 1]), true);
expect(containsDuplicate([1, 2, 3, 4]), false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment