Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

two sum javascript

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 ]
Comment

two sum javascript solution

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
  }
};
Comment

two sum js

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; 
    }
};
Comment

two sum js

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
Comment

two sum js

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 []
};
Comment

sum of 2 variables in javascript

 const  total = ((disAmount || 0) * 1) + ((TaxAmount|| 0) * 1) ;
Comment

two sum js

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)
Comment

two sum js

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)
Comment

two sum js

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)
Comment

two sum js

/**
 * @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 []; 
};
Comment

two sum js

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; 
    }
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery forEach is not a function 
Javascript :: react check if mounted 
Javascript :: the path argument must be of type string. received undefined react 
Javascript :: unexpected end of json input while parsing near 
Javascript :: append element in a div as first child 
Javascript :: calculate today date javascript 
Javascript :: npm stylelint 
Javascript :: addEventListener call only once 
Javascript :: Auto submit a form 
Javascript :: what is the type of children prop 
Javascript :: filter array of even numbers 
Javascript :: jquery find checkbox by value 
Javascript :: react apexcharts pie props 
Javascript :: example of pre increment in js 
Javascript :: can we add jquery in chrome extension js code 
Javascript :: sequelize update column type 
Javascript :: how to downgrade node version 
Javascript :: users api testing 
Javascript :: check if element is visible or hidden in dom 
Javascript :: JavaScript function that generates all combinations of a string. 
Javascript :: passing state in link react 
Javascript :: react native run ios release 
Javascript :: expo open app settings 
Javascript :: reactbootstrap multiselect 
Javascript :: click right mouse javascript 
Javascript :: react scrollTop smooth 
Javascript :: javascript if a variable is undefined 
Javascript :: javaSript string first words to upper case 
Javascript :: lazy react 
Javascript :: javascript remove style 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =