Array reemphasis is unavoidable in work. In the interview process, there is a high probability that there are so many possible methods in the market, so which one should be used and which is the optimal solution. Here’s a look at 👉 (do not force to use a method, suitable for their own is the best)
Define a new array and store the first element of the original array. Then compare the array of elements with the elements of the new array. If they differ, store them in the new array.
Low performance, better than stable, but not recommended
Sort the original array, compare it with its neighbors, and store it in the new array if it is different.
The following efficiency is not much different, in the pursuit of small amount of code and to achieve functional requirements, such a method is not recommended
Take advantage of the fact that an object property cannot be repeated, and if it does not exist, store it to a new array.
The array de-duplication method is problematic, extremely unstable and not recommended
Use the indexOf method of an array
Applicability is not strong, not suitable for multi-type array, slow speed
Includes method
Es6 method, recommended (this method available scenarios are very many and stable, very nice, provided that the browser supports ES6)
The filter and includes methods are used together
High and stable fault tolerance, no impact on performance, the same browser should support ES6
Splice method
Trouble, strongly not recommended (ants walking speed. Note: After deleting an element, you need to reduce the array length by 1.
Full, more…
LastIndexOf method
B: well... It's slow, not often used, but it can be used
The map method
Sort method
While this method can be used, it is not applicable, prone to problems, and not recommended
Set method
The most simple
That’s enough. Break up!
Two essential elements of array de-duplication, element comparison and flag recognition. That’s the difference between the original array and the new array. To implement de-duplication of arrays containing objects, we use methods that traverse the class (map, forEach, reduce, etc.). Changing arrays like Set, sort, etc. is generally not feasible