There are two arrays, for example:

mas1 = ["some1","some2","some3", "some4"];

mas2 = ["some1","some5","some3"];

It is necessary to compare these 2 arrays and output a unique value, in this case "some5"

I know how to merge arrays and output the result without duplicates, but how to compare 2 arrays and output a unique value is not.

Please help.

    3 answers 3

    use the classic solution. Get a hash (in javascript is a normal associative array), the keys of which are the elements of the array, and the values ​​are the number of repetitions. Then we go over each array and if there is no in the hash, add the key and the value 1. If the key is already there, increase the value by 1.

    Now, look for those keys in the hash, where the value is one. These will be unique values. This method is fast and hardly exists faster (in any case, you need to see all the elements of the array).

    This method can be improved. For the elements of the first array we increase by 1, and for the second by 2. Then in the hash for unique values ​​there will be either 1 or 2 (we immediately know where) and 3 for common elements. But if there are duplicate elements in the arrays, then there may be a bjaka. But it is solved very simply - you need to take not 1 and 2, but 1 and 1,000,000. And if there are less than a million repetitions - everything is ok.

    The method described by @TheDoctor is simple, but for large arrays it will be more blunt (because it has quadratic complexity).

    There is another way, which is to first sort the arrays, and then organize a "parallel pass" in two arrays. Coded a little difficult, but interesting.

    • The task did not say that the arrays are huge. But the method is higher .... - user31688
    • exactly. And then the browser eats several gigabytes of memory, blame the browser's prodvoditel ... And the programmer who “works, let it work, let the user deliver the memory strip and processor” is to blame - KoVadim
    • Keep it simple. The presented arrays are small. And on 1,000,000 I did not see, he is a perversion in itself. - user31688
    • I have not written that my method is better and should always be applied. I described when my code will be better. - KoVadim
    • Then OK, you're cool :) - user31688

    Use the Google, Luke!
    https://stackoverflow.com/a/4026828/4733017

     Array.prototype.diff = function(a) { return this.filter(function(i){return a.indexOf(i) < 0;}); }; var mas1 = ["some1","some2","some3", "some4"], mas2 = ["some1","some5","some3"]; var result = mas2.diff(mas1); console.info(result); // ["some5"] 

    • In fairness, "SO in Russian" was launched just for those who for some reason do not use the original SO. - Sergey Snegirev
    • In fairness, the answer is - they are also in the SO answer. SO.RU for Russian-speaking programmers, not Russian applicants. - user31688 pm

    You can also use Lodash. It is great for working with collections, such as difference: https://lodash.com/docs#difference

     _.difference([1, 2, 3], [4, 2]); // → [1, 3]