SHARE
TWEET

Untitled

a guest Jul 16th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getSumIndices = (arr, target) => {
  2.   if (target === undefined || arr.length < 2) return 0;
  3.  
  4.   const differences = {};
  5.   const result = [];
  6.  
  7.   for (let i = 0; i < arr.length; i++) {
  8.     const current = arr[i];
  9.  
  10.     if (differences[current] !== undefined) {
  11.       result.push([differences[current], i]);
  12.       delete differences[current];
  13.     } else {
  14.       const diff = target - current;
  15.  
  16.       if (!differences[current] && !differences[diff]) differences[diff] = i;
  17.     }
  18.   }
  19.  
  20.   return result;
  21. };
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top