The title

How to implement array deduplication?

Array = [1,5,2,3,4,2,3,1,3,4]

You’re going to write a function unique that makes

Unique (array) has the value [1,5,2,3,4]

That is, you get rid of all the duplicate values and keep only the non-duplicate values.

Written in the set

function unique(array){ return Array.from(new Set(array)); } the unique (,5,2,3,4,2,3,1,3,4 [1]) / /,5,2,3,4 [1]Copy the code

Cons: objects cannot be de-duplicated, and the API is relatively new, some older browsers may not support it

The Map method

unique = (arr) => { let hashMap = new Map(); let result = new Array(); for (let i = 0; i < arr.length; I ++) {if(hashmap. has(arr[I])) {if(arr[I]) { Set (arr[I], false); set(arr[I], false); set(arr[I], false); result.push(arr[i]); } } return result; } the unique (,5,2,3,4,2,3,1,3,4 [1]) / /,5,2,3,4 [1]Copy the code

Disadvantages: Objects cannot be de-weighted; The API for this method is also quite new, and some older browsers may not support it.