Skip to content

ContainsDuplicate #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Contains Duplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const containsDuplicate = function (nums) {
const arr = {};

nums.forEach((x) => {
if (arr[x]) {
return (arr[x] += 1);
} else {
return (arr[x] = 1);
}
});

const newArr = Object.keys(arr).map((x) => arr[x]);

return newArr.some((x) => x >= 2);
};
22 changes: 22 additions & 0 deletions TwoSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
// You can return the answer in any order.

const twoSum = function (nums, target) {
const obj = {};

for (let i = 0; i < nums.length; i++) {
const num = target - nums[i];

if (obj.hasOwnProperty(num)) {
return [obj[num], i];
}

obj[nums[i]] = i;
}
};

const nums = [3, 2, 3];
const target = 6;
const a = twoSum(nums, target);
console.log(a);