javascript - Compare two arrays of numbers and return array of highest values - Stack Overflow

What would be the most efficient way to pare two arrays of integers and return a unique array of the hi

What would be the most efficient way to pare two arrays of integers and return a unique array of the higher corresponding integers in each array?

(can use lodash)

ex:

var arr1 = [0, 1, 2, 1, 0];
var arr2 = [1, 0, 0, 3, 0];

// result = [1, 1, 2, 3, 0];

What would be the most efficient way to pare two arrays of integers and return a unique array of the higher corresponding integers in each array?

(can use lodash)

ex:

var arr1 = [0, 1, 2, 1, 0];
var arr2 = [1, 0, 0, 3, 0];

// result = [1, 1, 2, 3, 0];
Share Improve this question asked Sep 18, 2015 at 16:24 Jamie KudlaJamie Kudla 8723 gold badges18 silver badges37 bronze badges 3
  • Why are you worried about efficiency before you have figured out how to do it in the first place? What do you mean by a "unique" array? What if the arrays are of different length? – user663031 Commented Sep 18, 2015 at 16:28
  • Are you sure your sample code shows the correct result? "return a unique array" - You mean an array of unique values? – Adrian Lynch Commented Sep 18, 2015 at 16:30
  • Ah, I think I understand now. Compare the value at the same index and return the higher of the two. – Adrian Lynch Commented Sep 18, 2015 at 16:34
Add a ment  | 

5 Answers 5

Reset to default 3

Here's my first, less efficient method:

var arr1 = [0, 1, 2, 1, 0];
var arr2 = [1, 0, 0, 3, 0];

arr1.map(function (item, i) {
  return Math.max(item, arr2[i])
}); // [1, 1, 2, 3, 0]

What it lacks in speed, it gains in beauty!

Or for you ES6 boverers:

var arr1 = [0, 1, 2, 1, 0];
var arr2 = [1, 0, 0, 3, 0];

arr1.map((item, i) => Math.max(item, arr2[i])); // [1, 1, 2, 3, 0]
function selectHighestValues (arr1, arr2) {
  var maxLength = Math.max(arr1.length, arr2.length);
  var _arr = [];
  for (var i = 0; i < maxLength; i++) {
    if (arr1[i] && arr2[i]) {
      _arr[i] = Math.max(arr1[i], arr2[i]);
    } else if (arr1[i]) {
      _arr[i] = arr1[i]; 
    } else {
      _arr[i] = arr2[i]; 
    }

  }
  return _arr;
}

With splicing longest array and concatenation:

function selectHighestValues (arr1, arr2) {
  var minLength = Math.min(arr1.length, arr2.length);
  var maxLength = Math.max(arr1.length, arr2.length);
  var longestArrayIndex = arr1.length > arr2.length ? 0 : 1;
  var _arr = [];
  for (var i = 0; i < minLength; i++) {
    _arr[i] = Math.max(arr1[i], arr2[i]);
  }
  if (maxLength > minLength) {
    var sliced = Array.prototype.slice.call(arguments[longestArrayIndex], minLength, maxLength);
    _arr = Array.prototype.concat.call(_arr, sliced);
  }
  return _arr;
}

Brute force!

var arr1 = [0, 1, 2, 1, 0];
var arr2 = [1, 0, 0, 3, 0];
var result = [];

for(var i = 0; i < arr1.length; i++) {
  if (arr1[i] > arr2[i]) {
    result.push(arr1[i]);  
  } else {
    result.push(arr2[i]);
  }
}
_.zipWith(arr1, arr2, function(el1, el2) {
    return (el1 > el2 ? el1 : el2);
});

_.zipWith() is only in lodash 3.x I believe.

Using zip(), map(), and max():

_.map(_.zip(arr1, arr2), _.ary(_.max, 1));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744840434a4596524.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信