Symmetric wiki difference
We need to find the symmetric difference of the arrays.
I wrote a function for comparing two arrays, in which I combine arrays into one and look for duplicate values ββin a loop. When finding delete.
function sym() { var args = Array.prototype.slice.call(arguments); var result = compareTwoArray(arguments[0], arguments[1]); if (arguments.length > 2) { for (var i = 2; i < arguments.length; i++) { // console.log('result = ' + result); result = compareTwoArray(result, arguments[i]); } } return result; } function compareTwoArray() { var args = Array.prototype.slice.call(arguments); var result = []; var newArr = args.reduce(function(prev, curr) { return prev.concat(curr); }) for (var i = 0; i < newArr.length; i++) { var count = 0; for (var j = i+1; j < newArr.length; j++) { if (newArr[i] === newArr[j]) { count += 1; newArr.splice(j, 1); j -= 1; } } if (count === 0) { result.push(newArr[i]); } } return result; } console.log(sym([1, 2, 3], [3, 1, 5])); // [2, 5] console.log(sym([1, 1, 3], [4, 6])); // [1, 3, 4, 6] console.log(sym([1, 1, 2, 5], [2, 2, 3, 5])); // [1, 3] console.log(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])); // [1, 4, 5] console.log(sym([1, 2, 5], [2, 3, 5], [3, 4, 5])); // [1, 4, 5] I do not know how to handle duplicate values.
In one case, it must be removed.
[1, 2, 3] [3, 1, 5] [2, 5] - Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΏΠΎΠ»ΡΡΠΈΡΡΡΡ [2, 5] - ΠΏΠΎΠ»ΡΡΠ°Π΅ΡΡΡ Ρ ΠΌΠΎΠΈΠΌ ΠΊΠΎΠ΄ΠΎΠΌ In the other leave.
[1, 1, 3] [4, 6] [1, 3, 4, 6] - Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΏΠΎΠ»ΡΡΠΈΡΡΡΡ [3, 4, 6] - ΠΏΠΎΠ»ΡΡΠ°Π΅ΡΡΡ Ρ ΠΌΠΎΠΈΠΌ ΠΊΠΎΠ΄ΠΎΠΌ In the other and leave (1) and delete (2).
[1, 1, 2, 5] [2, 2, 3, 5] [1, 3] - Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΏΠΎΠ»ΡΡΠΈΡΡΡΡ [3] - ΠΏΠΎΠ»ΡΡΠ°Π΅ΡΡΡ Ρ ΠΌΠΎΠΈΠΌ ΠΊΠΎΠ΄ΠΎΠΌ 
console.log(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]));- Grundy[1, 1, 2, 5], [2, 2, 3, 5]=>[1, 3]. We get a new array and compare it with the following array[1, 3], [3, 4, 5, 5]=>[1, 4, 5]. I have two functions in the codecompareTwoArray()for comparison andsym()for returning the answer, in the case of arrays> 2, it is called in the cyclecompareTwoArray(), whereresultreturn from the previous value, andarguments[i]next array. - stackanon[1,4,5]and not[1,1,4,5,5]? - Grundy