var arr1 = [1,2,3,4,5,6,7]
var arr2 = [3,5,7,9,12,14]
Find the merged array of two arrays after removing duplicate elements (referring to deleting the duplicate elements on both sides, for example, if 3 is repeated, there cannot be 3 in the result, which is different from array deduplication), that is, the union of the arrays The part of subtracting the intersection, I don’t know if this part has any mathematical name. Looking for an algorithm with lower time complexity?
Use set as index and then traverse the shorter O(n)
To find the non-overlapping parts of two sets, you can first find the union of the two sets, and then remove the common elements.
A∪B - A∩B = { x | (x∈A & x∉B) || (x∉A & x∈B) }
The implementation using the program idea is:
Merge the two arrays, and then use the filter method to filter out the intersection elements in arrays a and b. Filter out the elements in a.concat(b) where a is not in b and where b is not in a. It mainly involves the judgment of whether an array contains an element. There are three implementation methods: Array.prototype.indexOf, Set's has method, and Array.prototype.includes.
Implementation method using ES5’s indexOf method without considering NaN:
Use the new Set collection method in ES6 and combine it with the filter method to filter the merged array.
Use the new Array.prototype.includes array method in ES7 to return whether an array contains specified elements, and combine it with the filter method to filter the merged array.
This result is called symmetric difference, which is defined on set or multiset
Complexity...generally
O(n)
----------I read the wrong question, the answer is wrong, the above is just the duplicates removed, see below-------------
----------You can get it done by just traversing the basic for loop---------------
The first method, for
The second type, concat and filter, the principle is to merge and then remove duplicates
Using the properties of objects to implement
ScreenShot