1

What would be the most efficient way to compare 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];
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? 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? 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. Commented Sep 18, 2015 at 16:34

5 Answers 5

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Doncha mean const for the latter :p
2
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;
}

3 Comments

Won't you get an undefined offset using maxLength if one array is shorter than the other?
Fixed. If one array shorter then other, function will just copy values from longest array.
Added new method with splicing longest array and concatenation.
1

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]);
  }
}

1 Comment

Phooey, I was hoping for a recursive, functional solution.
1
_.zipWith(arr1, arr2, function(el1, el2) {
    return (el1 > el2 ? el1 : el2);
});

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.