Method 1:
function unique1 (array) {
var newArr = [];
Traversal array for
(let key of array) {
//If key has been saved to a temporary array, skip it, otherwise save it to a temporary array if
(Newarr.indexof (key) = = 1) {
Newarr.push (key)
}
return
NEWARR
}
Method 2:
function Unique2 (array) {
//n is a hash table, NEWARR is a temporary array
var n = {},newarr = [];
For (let key of array) {if
(!n[key]) { //If there is no current item in the hash table
N[key] = true; Depositing hash table
Newarr.push (key); Push the current array item to a temporary array
} return
newArr;
}
Note: cannot distinguish between 1 and ' 1 '
Unique2 ([1,2,4, ' 1 ', 2,4,3,2] // [1, 2, 4, 3]
Method 3: Using the Filter method
function Unique3 (array) {var newArr = Array.filter (function (element, index, self) {
return Self.indexof (Element) = = = Index});
return NEWARR; }