diff --git a/ Contains Duplicate.js b/ Contains Duplicate.js new file mode 100644 index 0000000..1121971 --- /dev/null +++ b/ Contains Duplicate.js @@ -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); +}; diff --git a/TwoSum.js b/TwoSum.js new file mode 100644 index 0000000..27013e2 --- /dev/null +++ b/TwoSum.js @@ -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);