Javascript Array twoSum() method

Description

Javascript Array twoSum() method


Array.prototype.twoSum = function () {
  twoSums = [];/*from  w w w.  j av a2s.c  om*/

  for( var i = 0; i < this.length; i++) {
    for( var j = i+1; j < this.length; j++) {
      if( (this[i] + this[j]) === 0) {
        twoSums.push([i,j])
      };
    };
  };
  return twoSums;
};

console.log([-1,0,2,-2,1].twoSum());

Javascript Array twoSum()

let arr = [-2,0,1,2,-1];
let arr2 = [-1,0,2,-2,1];

Array.prototype.twoSum = function () {
  let result = [];

  for (let i = 0; i < this.length; i += 1) {      //this refers to the ARRAY
    for (let j = (this.length - 1); j > i; j -= 1) {
      if (this[i] + this[j] === 0) {
        result.push([i,j]);/* w w w. j  a  v a  2s  . co m*/
      }
    }
  }
  return result;
}

console.log(arr.twoSum());
console.log(arr2.twoSum());

Javascript Array twoSum()

Array.prototype.twoSum = function() {
  pos = [];/*from  ww  w .  ja  v a 2s .  c  o  m*/
  for (var i = 0; i < this.length; i++) {
    for (var j = (i + 1); j < this.length; j++) {
        if (this[i] + this[j] === 0) {
          pos.push([i, j]);
        }
    }
  }

  return pos;
}

Javascript Array twoSum()

Array.prototype.twoSum = function () {
  let results = [];
  for (let i = 0; i < this.length; i += 1){
    for (let j = (i+1); j < this.length; j += 1){
      if (this[i] + this[j] === 0){
        results.push([i,j]);// ww  w .j  a va2s  .co m
      }
    }
  }
  return results;
};


console.log([-1, 0, 2, -2, 1].twoSum());

Javascript Array twoSum()

// two sum//from   w  ww . j  a v  a 2  s  .  c  o  m
Array.prototype.twoSum = function() {
  for (var i = 0; i < this.length; i++) {
    for (var j = (i + 1); j < this.length; j++) {
      if (this[i] + this[j] === 0) {
        return true;
      }
    }
  }

  return false;
};

Javascript Array twoSum()

Array.prototype.twoSum = function () {
  var indices = [];
  for (var i = 0; i < (this.length - 1); i++) {
    for (var j = (i + 1); j < this.length; j++) {
      if (this[i] + this[j] === 0) {
        indices.push([i, j]);//w  ww.  ja  v a 2s. co  m
      }
    }
  }
  return indices;
};

Javascript Array twoSum()

// Write a method, `Array#two_sum`, that finds all pairs of positions where the
// elements at those positions sum to zero.
////from   w  w  w .  ja  va2s .  co  m
// NB: ordering matters. I want each of the pairs to be sorted smaller index
// before bigger index. I want the array of pairs to be sorted
// "dictionary-wise":
// [0, 2] before [1, 2] (smaller first elements come first)
// [0, 1] before [0, 2] (then smaller second elements come first)

Array.prototype.twoSum = function () {
  const pairs =[];

  for (var i = 0; i < this.length; i++) {
    for (var j = i+1; j < this.length; j++) {
      if (this[i] + this[j] === 0) {
        pairs.push([i,j]);
      }
    }
  }
  return pairs;
};

console.log([1,4,6,-1,7,3,-4].twoSum()); //[[0,3], [1,6]]

Javascript Array twoSum()

var uniq = function (arr) {
  var result = [];

  for(var i = 0; i < arr.length; i++){
    if (result.indexOf(arr[i]) === -1) {
      result.push(arr[i]);/*from  w  w  w .ja  v a 2s .  c o  m*/
    }
  }

  return result;
};


Array.prototype.twoSum = function() {
  var result = [];

  for(var i = 0; i < this.length; i++) {
    for(var j = i + 1; j < this.length; j++) {
      if(this[i] + this[j] === 0) {
        result.push([i, j]);
      }
    }
  }

  return result;
};

var myTranspose = function(arr) {
  var result = new Array(arr[0].length);

  for(var i = 0; i < arr.length; i++) {
    result[i] = new Array(arr.length);
  }

  for(var row = 0; row < arr.length; row++){
    for(var col = 0; col < arr[0].length; col++){
      result[col][row] = arr[row][col];
    }
  }

  return result;
};

Javascript Array twoSum()

Array.prototype.twoSum = function () {
  const pairs = [];// www  .j  a v  a 2 s  . c om

  for (let i = 0; i < this.length; i++){
    for (let j = (i + 1); j < this.length; j++){
      if(this[i] + this[j] === 0){
        pairs.push([i , j]);
      }
    }
  }

  return pairs;
};
console.log([-1, 0, 2, -2, 1].twoSum());

Javascript Array twoSum()

Array.prototype.twoSum = function() {
  var output = [];

  for(var i in this) {
    for(var j in this) {
      if( (i < j) && (this[i] + this[j] === 0) ) {
        output.push([i,j]);// w ww.  jav  a 2s.  co m
      }
    };
  };

  return output;
};

Javascript Array twoSum()

Array.prototype.twoSum = function() {
  var result = [];
  for(var i = 0; i<this.length - 1; i++){
    for(var j = i + 1; j < this.length; j++){
      if (this[i] + this[j] === 0) {
        result.push([i,j]);//from  www .  j  a  v a2s . com
      }
    }
  }
  
  return result;
}

// console.log([-2, -1, 0, 1, 2].twoSum())
// Could do is this way or use Array.prototype as well
var myTranspose = function(arr) {
  var result = []
  for(var i = 0; i < arr.length; i++){
    result.push([])
  }
  
  for(var j = 0; j < arr.length; j++){
    for(var k = 0; k < arr[j].length; k++){
      result[k].push(arr[j][k])
    }
  }
  
  return result;
}

Javascript Array twoSum()

Array.prototype.twoSum = function() {
  let idxPairs = [];
  for(let i = 0; i < this.length - 1; i++) {
    for(let j = i + 1; j < this.length; j++) {
      if(this[i] + this[j] === 0) {
        idxPairs.push([i, j]);/*from  w w  w.  j a v a2s . c  o m*/
      }
    }
  }
  return idxPairs;
}

Javascript Array twoSum()

Array.prototype.twoSum = function () {
  const pairs = [];//from  w  ww. j av a  2s . c  o  m
  const dict = {};
  for (let i = 0; i < this.length; i++) {
    let x = this[i];
    if (!(x in dict)) {
      dict[-x] = i;
    } else {
      pairs.push([dict[x], i]);
    }
  }
  return pairs;
};

Javascript Array twoSum()

// twoSum//  www. ja v a 2  s . com
Array.prototype.twoSum = function () {
  const pairs = [];

  for (let i = 0; i < this.length; i++) {
    for (let j = (i + 1); j < this.length; j++) {
      if (this[i] + this[j] === 0) {
        pairs.push([i, j]);
      }
    }
  }

  return pairs;
};

console.log([-1, 0, 2, -2, 1].twoSum());



PreviousNext

Related