SHARE
TWEET

Untitled

a guest Jul 17th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function foo(nums,target)
  2. {
  3.     let return_arr=[];
  4.   let mapping = {};
  5.   for (let i =0; i <nums.length;i++)
  6.   {
  7.     mapping[nums[i]] = i;
  8.   }
  9.  
  10.   for (let i =0; i <nums.length;i++)
  11.   {
  12.       if (nums[i] in mapping && mapping[nums[i]]>-1)
  13.       {
  14.         //  console.log("pushing");
  15.           mapping[nums[i]] = -1;
  16.           let lookfor = target - nums[i];
  17.          
  18.           if  (lookfor in mapping && mapping[lookfor]>-1){
  19.               let lookfor_index = mapping[lookfor];
  20.                mapping[lookfor] =-1;
  21.                console.log("pushing: "+ i + " and " + lookfor_index);
  22.                return_arr.push([i,lookfor_index]);
  23.           }
  24.       }
  25.   }
  26.  
  27.   return return_arr;
  28. }
  29.  
  30.  
  31. //let a = foo([1, 3, 4, 5, 6, 8, 10, 11, 13],14);
  32.  
  33. //console.log(a);
  34. // [0, 8], [1, 7], [2, 6], [4, 5]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top