0

For example

inputarray = [1, 4, 5, 9];
array1 = [1, 2, 3, 4, 5, 6, 7];
array2 = [8, 9, 10, 11, 12, 13, 14];

I would like to match inputarray with both array1 & array2.In this current scenario 1,4,5 belongs to the array1 and 9 belongs to the second array.I am expecting the output like

outputarray1=[1,4,5]
outputarray2=[9]

Suggest me best way to do this problem.I mean with less complexity.Thanks in advance.

5
  • if you have a complex solution already done... share it... else have you tried anything Commented Jul 8, 2015 at 7:12
  • Refer to this stackoverflow.com/questions/11286979/… the complexity for this answer is N. Commented Jul 8, 2015 at 7:14
  • What if array1 and array2 overlap? What if they are the same? What's the current complexity? Do you mean "less complexity" in terms of asymptotic behaviour? Or in terms of performance? How is this related to node.js? Commented Jul 8, 2015 at 7:15
  • @JosuaMarcelChrisano: For a single item. For k items it's usually O(n * k), unless you have some precondition. Commented Jul 8, 2015 at 7:17
  • Possible solution link stackoverflow.com/questions/1885557/… Commented Jul 8, 2015 at 7:17

4 Answers 4

1

At the cost of space, you can make hash objects and have constant time lookups for contains instead of using O(n) indexOf calls or for loops:

var inputarray = [1, 4, 5, 9];
var array1 = [1, 2, 3, 4, 5, 6, 7];
var array2 = [8, 9, 10, 11, 12, 13, 14];

var array1Hash = Object.create(null);
var array2Hash = Object.create(null);

var outputarray1 = [];
var outputarray2 = [];

array1.forEach(function(e) {
  array1Hash[e] = true;
});
array2.forEach(function(e) {
  array2Hash[e] = true;
});

inputarray.forEach(function(e) {
  if (e in array1Hash) {
    outputarray1.push(e);
  }
  if (e in array2Hash) {
    outputarray2.push(e);
  }
});

document.getElementById('out1').innerHTML = JSON.stringify(outputarray1);
document.getElementById('out2').innerHTML = JSON.stringify(outputarray2);
<pre id="out1"></pre>
<pre id="out2"></pre>

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Array.prototype.filter for it:

var inputarray = [1,4,5,9];
var array1 = [1,2,3,4,5,6,7];
var array2 = [8,9,10,11,12,13,14];

function matchEqualElements(arrayToCompare) {

    return function (element) {
        return (arrayToCompare.indexOf(element) !== -1);
    }
}

console.log(inputarray.filter(matchEqualElements(array1)));

console.log(inputarray.filter(matchEqualElements(array2)));

Comments

0

You can use this

var inputarray = [1, 4, 5, 9],
     array1 = [1, 2, 3, 4, 5, 6, 7],
     array2 = [8, 9, 10, 11, 12, 13, 14],
    outputarray1=[],outputarray2=[];

     inputarray.map(function(item){
       for(var i=0;i<array1.length;i++){
         if(array1[i]==item)outputarray1.push(item);
       }
       for(var i=0;i<array2.length;i++){
         if(array2[i]==item)outputarray2.push(item);
       }
     });
    alert(outputarray1);
    alert(outputarray2);

Comments

0

Complexity in terms of less code? There's a library for that: Underscore#intersection.

inputarray = [1, 4, 5, 9];
array1 = [1, 2, 3, 4, 5, 6, 7];
array2 = [8, 9, 10, 11, 12, 13, 14];    
_.union(inputarray, array1);
_.union(inputarray, array2);

Output:

=> [1, 4, 5]
=> [9]

Algorithmic complexity, you're probably looking at O(n*m) worst case, but you could optimize with sorted arrays.

function intersection(a1, a2) {
  var results = [],
    i = 0,
    j = 0;

  while (i < a1.length && j < a2.length) {
    if (a1[i] === a2[j]) {
      results.push(a1[i]);
      i++;
      j++;
    } else if (a1[i] > a2[j]) {
      j++;
    } else if (a1[i] < a2[j]) {
      i++;
    }
  }

  return results;
}

console.log(intersection([1, 2, 3, 4, 5], [3, 5, 6, 7]));
// [3, 5]
console.log(intersection([1, 2, 3, 4, 5], [1, 2, 5, 6]));
// [1, 2, 5]

This gets you O(n+m) which if n >>> m, is just O(n).

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.