xxxxxxxxxx
var twoSum = function(nums, target) {
const indicies = {}
for (let i = 0; i < nums.length; i++) {
const num = nums[i]
if (indicies[target - num] != null) {
return [indicies[target - num], i]
}
indicies[num] = i
}
};
xxxxxxxxxx
const nums = [3, 4, 5, 6, 7, 3]
const target = 6
const twoSum = function(nums, target) {
let map = {}
for (let i = 0; i < nums.length; i++) {
if (target - nums[i] in map) {
return [map[target - nums[i]], i]
} else {
map[nums[i]] = i
}
}
return []
}
console.log(twoSum(nums, target)) // [Log]: [ 0, 5 ]
xxxxxxxxxx
function sumDigit(nums, target) {
let n = nums.length;
for(let i = 0; i < n - 1; i++) {
for(let j = i + 1; j < n; j++) {
if(nums[i] + nums[j] === target) return [i,j]
}
}
return []
}
sumDigit([3,2,4], 6)
xxxxxxxxxx
function twoSum(nums, target) {
const viewed = []; // access by index is faster that Map
for(let i=0; ;i++){ //according to task description - solution is always present - no need to check <.length or something like this
const current = nums[i];
const j = viewed[current];
if(j !== undefined){
return [i, j];
}
viewed[target - current] = i;
}
};
xxxxxxxxxx
function sumDigit(nums, target) {
let n = nums.length;
for(let i = 0; i < n-1; i++) {
for(let j = i+1; j < n; j++) {
if(nums[i] + nums[j] === target) return [i,j]
}
}
return []
}
sumDigit([3,2,4], 6)
// n-1 = last index dropped if -> n+1 add new index
// nums[i] = 332
// nums[j] = 244
// nums[i] + nums[j] -> 3+2 = 5 | 3+4 = 7 | 2+4 = 6
xxxxxxxxxx
var twoSum = function(nums, target) {
let mapping = {}
for(let i = 0; i < nums.length; i++) {
if(target - nums[i] in mapping) {
return [mapping[target-nums[i]],i]
} else {
mapping[nums[i]] = i
}
}
return []
};
xxxxxxxxxx
//Uses an arrow function, which is a more concise way of defining functions in JavaScript.
//You don't need to use return in JavaScript in the arrow function And JavaScript itself returns automatically
const addNumbers = (num1, num2) => num1 + num2;
// Example usage:
const result = addNumbers(5, 3);
console.log(result); // Output: 8
xxxxxxxxxx
var sumDigit = function(nums, target) {
const indexArr = [];
let i = 0
while(true) {
const current = nums[i];
const j = indexArr[current];
if(j !== undefined) return [i, j];
indexArr[target - current] = i;
i++
}
};
sumDigit([3,2,4], 6)
xxxxxxxxxx
function sumDigit(nums, target) {
let mapping = {}
for(let i = 0; i < nums.length; i++) {
let n = nums[i]
if(mapping[target - n] != null) return [ mapping[target - n], i ]
mapping[n] = i
}
}
sumDigit([3,2,4], 6)
xxxxxxxxxx
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
// return brute(nums,target);
return memoization(nums, target);
};
// Naive Approach: Brute Force | O(N^2) | O(1)
const brute = (nums, target) => {
for (let i = 0; i < nums.length - 1; i++) {
for (let j = i+1; j < nums.length; j++) {
if (nums[i]+nums[j] === target) return [i,j];
}
}
return []
};
// Memoization: HashMap | O(N) | O(N)
const memoization = (nums, target) => {
const memo = {};
for (let i = 0; i < nums.length; i++) {
if (Number.isInteger(memo[nums[i]])) return [memo[nums[i]], i];
else memo[target - nums[i]] = i;
}
return [];
};