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

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 :: redis set expire time node js 
Javascript :: wordpress disable jquery migrate 
Javascript :: mathjax new line 
Javascript :: substring 
Javascript :: jquery form data 
Javascript :: Repeat a String Repeat a String 
Javascript :: jsx classname multiple 
Javascript :: convert image object to blob javascript 
Javascript :: js get html content 
Javascript :: best and fastest encrypt and decrypt value in javascript 
Javascript :: add regexp to antd 
Javascript :: filter by keyname javascript 
Javascript :: emailjs react npm 
Javascript :: formik react native 
Javascript :: javascript template literals 
Javascript :: button not exist js 
Javascript :: findbyid mongoose 
Javascript :: how to make input field empty in javascript 
Javascript :: How to replace a value in localstorage using javascript 
Javascript :: query string from object js 
Javascript :: javascript get day 
Javascript :: chart js radar grid color 
Javascript :: export all javascript 
Javascript :: react native share image 
Javascript :: error An unexpected error occurred: "https://registry.yarnpkg.com/@material-ui/icons/-/icons-4.11.2.tgz: ESOCKETTIMEDOUT". 
Javascript :: sequelize sqlite example 
Javascript :: string to json js 
Javascript :: react-file-base64 
Javascript :: html get class property 
Javascript :: JavaScript Create Multiple Objects with Constructor Function 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =