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 :: p5.js how to display a text from string 
Javascript :: javascript change _ to space 
Javascript :: bootstrap multiselect dropdown with checkbox 
Javascript :: sanitize html in javascript 
Javascript :: material ui icons next js 
Javascript :: javascript decimals without rounding 
Javascript :: arr.sort 
Javascript :: js object from array of keys 
Javascript :: if js 
Javascript :: use moment js in ejs file 
Javascript :: Create JavaScript Strings 
Javascript :: angular property value does not exist on type Event 
Javascript :: Select HTML elements by CSS selectors 
Javascript :: how to create a class javascript 
Javascript :: get min/max array 
Javascript :: react tailwind loading 
Javascript :: Setting axios base url dynamically 
Javascript :: expo react navigation 
Javascript :: materialize dropdown js 
Javascript :: how to load js in vuejs components 
Javascript :: push in object javascript 
Javascript :: get % of number javascript 
Javascript :: math random javascript 
Javascript :: how to convert div to image in jquery 
Javascript :: how to receive window.postmessage event in angular 9 
Javascript :: Promise.all() with async and await 
Javascript :: change events 
Javascript :: mongodb find and update one field 
Javascript :: js ternary else if multi 
Javascript :: infinite scroll for chat react js 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =