1. Compare each element in the array with other elements. If duplicate elements are found, delete them

var arr = [1.23.1.1.1.3.23.5.6.7.9.9.8.5.5.5.5];
console.log(arr);    23, / / [1, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5, 5, 5, 5]
function noRepeat1(arr) {
    for(var i = 0; i < arr.length-1; i++){
        for(var j = i+1; j < arr.length; j++){
            if(arr[i]===arr[j]){
                arr.splice(j,1); j--; }}}return arr;
}
var arr2 = noRepeat1(arr);
console.log(arr2);    //[1, 23, 3, 5, 6, 7, 9, 8]
Copy the code

2. Use indexOf() to determine whether the indexOf the element’s first occurrence in the array is the same as the indexOf the loop

var arr = [1.23.1.1.1.3.23.5.6.7.9.9.8.5.5.5];
console.log(arr);    23, / / [1, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5, 5, 5]
function noRepeat2(arr) {
    for (var i = 0; i < arr.length; i++) {
        if(arr.indexOf(arr[i]) ! = i) { arr.splice(i,1);// The element after the array length is reduced by 1 moves forward
            i--;// Array subscript rollback}}return arr;
}
var newArr = noRepeat2(arr);
console.log(newArr);    //[1, 23, 3, 5, 6, 7, 9, 8]
Copy the code

3. Use the filter method in the array

var arr = ['apple'.'banana'.'pear'.'apple'.'orange'.'orange'];
console.log(arr)    //["apple", "banana", "pear", "apple", "orange", "orange"]
var newArr = arr.filter(function(value,index,self){
return self.indexOf(value) === index;
});
console.log(newArr);    //["apple", "banana", "pear", "orange"]
Copy the code

4. Use the new array to determine if the indexOf the current element in the array is equal to the indexOf the loop to add to the new array

var arr = [1.23.1.1.1.3.23.5.6.7.9.9.8.5.5.5];
console.log(arr)    23, / / [1, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5, 5, 5]
function noRepeat4(arr) {
    var ret = [];
    for (var i = 0; i < arr.length; i++) {
        if(arr.indexOf(arr[i]) == i) { ret.push(arr[i]); }}return ret;
}
var arr2 = noRepeat4(arr);
console.log(arr2);    //[1, 23, 3, 5, 6, 7, 9, 8]
Copy the code

5. Use empty objects to record the elements already stored in the new array

var arr = [1.23.1.1.1.3.23.5.6.7.9.9.8.5];
console.log(arr)    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
var obj={};
var newArr=[];
for(var i=0; i<arr.length; i++){if(! obj[arr[i]]){ obj[arr[i]]=true; newArr.push(arr[i]); }}console.log(newArr);    //[1, 23, 3, 5, 6, 7, 9, 8]
Copy the code

6. Use the new array to check whether the element exists in the new array. If not, add the element to the new array

var arr = [1.23.1.1.1.3.23.5.6.7.9.9.8.5];
console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
function noRepeat6(arr){
    var newArr = [];
    for(var i = 0; i < arr.length; i++){
        if(newArr.indexOf(arr[i]) == -1){ newArr.push(arr[i]); }}return newArr;
}
var arr2 = noRepeat6(arr);
console.log(arr2);    //[1, 23, 3, 5, 6, 7, 9, 8]
Copy the code

7. Use the new array to determine whether the element exists in the new array. If not, add the element to the new array (the original array is the same length but sorted by string order).

var arr = [1.23.1.1.1.3.23.5.6.7.9.9.8.5];
console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
function noRepeat7(arr) {
    var ret = [],
        end;// Temporary variables are used to compare repeating elements
    arr.sort();// Regroup the numbers
    end = arr[0];
    ret.push(arr[0]);
    for (var i = 1; i < arr.length; i++) {
        if(arr[i] ! = end) {// Add the current element to the new array if it is not the same as the temporary elementret.push(arr[i]); end = arr[i]; }}return ret;
}
var arr2 = noRepeat7(arr);
console.log(arr2);    //[1, 23, 3, 5, 6, 7, 8, 9]
Copy the code

8. This method changes the original array without using the new array, and the array is sorted

var arr = [1.23.1.1.1.3.23.5.6.7.9.9.8.5];
console.log(arr);    //[1, 23, 1, 1, 1, 3, 23, 5, 6, 7, 9, 9, 8, 5]
function noRepeat8(arr) {
    var end;// Temporary variables are used to compare repeating elements
    arr.sort();// Regroup the numbers
    end = arr[0];
    for (var i = 1; i < arr.length; i++) {
        if (arr[i] == end) {// The current element is removed from the array if it is equal to the temporary element
            arr.splice(i,1);
            i--;
        }else{ end = arr[i]; }}return arr;
}
var arr2 = noRepeat8(arr);
console.log(arr2);    //[1, 23, 3, 5, 6, 7, 8, 9]
Copy the code

9. A double-layer loop changes the array

var arr = [1.1.2.2.3.3.4.4.5.5.4.3.1.2.6.6.6.6];
console.log(arr);    / / (1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 3, 1, 2, 6, 6, 6, 6]
function noRepeat9(arr){
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < arr.length; j++) {
            if(arr[i] == arr[j] && i ! = j) {// delete the duplicate number
                arr.splice(j, 1); }}}return arr;
}
var arr2  = noRepeat9(arr);
console.log(arr2);    //[1, 2, 3, 4, 5, 6]
Copy the code

10. Use a new array

var arr = [1.1.2.2.3.3.4.4.5.5.4.3.2.1.1.1];
console.log(arr);    / / (1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 3, 2, 1, 1, 1)
var newArr = [];
for (var i = 0; i < arr.length; i++) {
    var repArr = [];// Receive the subscript after repeated data
    // The inner loop finds the subscripts with duplicate data
    for (var j = i + 1; j < arr.length; j++) {
        if (arr[i] == arr[j]) {
            repArr.push(j);// find the subscript of the following repeated data}}//console.log(repArr);
    if (repArr.length == 0) {// If the duplicate array has no value, it is not duplicate datanewArr.push(arr[i]); }}console.log(newArr);    //[5, 4, 3, 2, 1]
Copy the code

11. Use the Set structure provided by ES6

var arr = [1.1.2.2.3.3.4.4.5.5.4.3.2.1.1.1];
console.log(arr);    / / (1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 3, 2, 1, 1, 1)
function noRepeat11(arr){
    var newArr = [];
    var myset = new Set(arr);// Set can't accept duplicate data
    for(var val of myset){
        newArr.push(val)
    }
    return newArr;
}
var arr2 = noRepeat11(arr)
console.log(arr2);    //[1, 2, 3, 4, 5]
Copy the code