0

I would like to know how to compare two or more -- potentially unlimited -- arrays for common values and push these values into a new array. Below I have a function that will accept unlimited arguments, but I am uncertain if this is a good place to begin. PHP appears to have a method that can do what I want called array_intersect. Does javascript offer something similar?

edit: I have found examples of how this can be done with two or so arrays, but I have not found examples of how these approaches might be applied to an unspecified number of arrays as of yet. Therefore I do not see this as a duplicate question.

var sampleOne = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var sampleTwo = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];

function FindDirectRelation() {
    for(var i = 0; i < arguments.length; ++i) {
        console.log(arguments[i]);
        

    };
};

var directRelation = FindDirectRelation(sampleOne, sampleTwo);

I am still a coding novice, so please ensure that everything is explained in a way that is simple enough for me to understand.

Laurence
  • 43
  • 3
  • This is not a duplicate. The answers in the question that you linked to are at best limited to a specified number of arrays. I want to compare an unspecified number of arrays efficiently. The PHP array_intersect does this very nicely. – Laurence Dec 21 '15 at 04:43
  • you can use the winner-function in the thread Tushar linked to and use it at the leaves of a binary splitting algorithm. So, not really a duplicate, but who gives... – deamentiaemundi Dec 21 '15 at 05:00
  • baby steps: strip each input array to unique values, concat all the input arrays, filter for anything that appears more than once – dandavis Dec 21 '15 at 05:07
  • @dandavis The way you detail does make sense to me, but I really do not have enough experience to pull this off without guidance. I also question if it is the best way. edit: For instance, what if one array contains two more of the same values? – Laurence Dec 21 '15 at 05:21
  • well, my simple method uses unique inputs, that is you need to strip dupes before the concat. but there's another way entirely i forgot about: if you want values common to every input array: `[[1,2,3],[1,3,4,5],[5,1,3]].reduce(intersect)`, where _intersect_ is, eg. http://stackoverflow.com/a/33945278/2317490 ... – dandavis Dec 21 '15 at 05:28
  • @deamentiaemundi I just want something that I can make sense of. I fail to see how the chosen answer for the linked question can in any way help with my question. – Laurence Dec 21 '15 at 05:31
  • @Laurence I would have trouble doings so without showing code, at least not in the 500 comment characters, so pleeease ask a new question. And don't ask the very same question, summarize where you are now, where your problems are, what code you have and so on. An what I meant: you can compare two arrays, so you can compare two array and another two array and take the two results and compare them. All arrays must be sorted if you want it done efficient. Sort the input arrays according to their length, too, and compare biggest arrays with the smallest, rinse and repeat. – deamentiaemundi Dec 21 '15 at 14:59

0 Answers0