Fixed code structure

Method 1 uses the tool library Lodash/Underscore

Both tools are used in the same way, but the libraries referenced are different!

Method 2 ES6 Set method

So we’re going to create a New Set and we’re going to put an array in it, and the thing about a Set is that the elements in it are unique, but now it’s a Set object, not an array, and now we need to convert that Set object into an arrayPrinted results:

Array from()

2 Es6 expansion operator [… XXX]

Method three indexOf judgment

Create a new array result, use the forEach loop to iterate through arR, return -1 if indexOf does not exist, return the initial subscript if it does exist, and then push the nonexistent array to the new array.

Method 4 filter

For example, if there are two 4’s in item, indexOf will first find the 4 with the index of 1. When detecting the 4 with the index of 4, indexOf will return the index of 1. I don’t return the 4 with index 4, so I filter out duplicates.

Methods five reduce

Pre is the result of the previous loop, and [] is the initial value. Each loop uses pre-. Includes to determine whether the current item is in Pre.

Method six uses a unique feature of the object’s key value

First declare an empty object, and use forEach to store the array value as the key of the empty object. Since it has nothing to do with value, we write a random SSS. Since the key value in the object is unique, so now all saved are deduplicated. We then take the key out again, use object.keys (), and use map to convert the result into an array.

The Boolean value is changed to a numeric value, that is, ~~true === 1, ~~false === 0.

Method 7: The original method doubles the for loop

Compare the current number with all subsequent numbers, discard the current number if there are duplicates, and keep the current number if there are no duplicates.

Method 8 sort

Use sort, and then use the for loop to determine if two adjacent numbers are the same. Use the reverse thinking, if they don’t push, they don’t do anything.