11 ways to remove duplicates from js arrays

11 ways to remove duplicates from js arrays

In actual work or interviews, we often encounter the problem of "array deduplication". The following are various methods of array deduplication implemented using js:

1. Compare each element of the array with other elements in turn, find duplicate elements, and delete them

var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5,5,5,5];
    console.log(arr); //[1, 23, 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]

2. Use the indexOf() method to determine whether the position subscript of the first appearance of this element in the array is equal to the loop subscript

var arr = [1,23,1,1,1,3,23,5,6,7,9,9,8,5,5,5];
    console.log(arr); //[1, 23, 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);//After deleting the array element, the array length is reduced by 1 and the following elements are moved forward i--;//array subscript rollback}
        }
        return arr;
    }
    var newArr = noRepeat2(arr);
    console.log(newArr); //[1, 23, 3, 5, 6, 7, 9, 8]

3. Using 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"]

4. Use the new array to determine the index of the current element in the array through the indexOf method. If it is equal to the subscript of the loop, add it 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) //[1, 23, 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]

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]

6. With the help of the new array, determine 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]

7. With the help of the new array, determine whether the element exists in the new array. If not, add the element to the new array (the length of the original array remains unchanged but is sorted in 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 variable used to compare repeated elements arr.sort(); //re-sort the numbers end = arr[0];
        ret.push(arr[0]);
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] != end) {//If the current element is not equal to the temporary element, add this element to the new array ret.push(arr[i]);
                end = arr[i];
            }
        }
        return ret;
    }
    var arr2 = noRepeat7(arr);
    console.log(arr2); //[1, 23, 3, 5, 6, 7, 8, 9]

8. This method does not directly change the original array with the help of a new array, and the array after deduplication 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 variable used to compare repeated elements arr.sort(); //re-sort the numbers end = arr[0];
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] == end) {//If the current element is equal to the temporary element, delete this element from the array 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]

9. Double loop changes the original 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 repeated numbers arr.splice(j, 1);
                }
            }
        }
        return arr;
    }
    var arr2 = noRepeat9(arr);
    console.log(arr2); //[1, 2, 3, 4, 5, 6]

10. With the help of 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 the repeated data //Inner loop finds the subscript with repeated 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 repeated array has no value, it means it is not repeated data newArr.push(arr[i]);
        }
    }
    console.log(newArr); //[5, 4, 3, 2, 1]

11. With the help of 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); //Using the feature that the Set structure cannot receive duplicate data for(var val of myset){
            newArr.push(val)
        }
        return newArr;
    }
    var arr2 = noRepeat11(arr)
    console.log(arr2); //[1, 2, 3, 4, 5]

The above are the details of 11 methods for js array deduplication. For more information about js array deduplication, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Nine advanced methods for deduplicating JS arrays (proven and effective)
  • Detailed examples of JS array flattening, deduplication, and sorting operations
  • JS array attribute deduplication and verification of duplicate data
  • High-performance js array deduplication (12 methods, the most comprehensive in history)
  • Example of object deduplication in JS array
  • Summary of js array deduplication methods
  • 6 methods of deduplication in JS arrays with complete examples
  • N methods to remove duplicates from js arrays (summary)
  • Summary of common methods for deduplication of JS arrays [4 methods]
  • JS array deduplication details

<<:  MySQL slow log online problems and optimization solutions

>>:  Linux redis-Sentinel configuration details

Recommend

Implementation of MySQL Multi-version Concurrency Control MVCC

Table of contents What is MVCC MVCC Implementatio...

CSS3 uses var() and calc() functions to achieve animation effects

Preview knowledge points. Animation Frames Backgr...

Detailed analysis of the syntax of Mysql update to modify multiple fields and

When updating a record in MySQL, the syntax is co...

The easiest way to reset mysql root password

My mysql version is MYSQL V5.7.9, please use the ...

How to install openssh from source code in centos 7

Environment: CentOS 7.1.1503 Minimum Installation...

Detailed explanation of HTML page header code example

Knowledge point 1: Set the base URL of the web pa...

How to disable the automatic password saving prompt function of Chrome browser

Note: In web development, after adding autocomplet...

The difference between HTML name id and class_PowerNode Java Academy

name Specify a name for the tag. Format <input...

JavaScript to achieve simple image switching

This article shares the specific code for JavaScr...

JS realizes automatic playback of timeline

Recently, I have implemented such an effect: clic...

Tutorial on disabling and enabling triggers in MySQL [Recommended]

When using MYSQL, triggers are often used, but so...

Javascript Basics: Detailed Explanation of Operators and Flow Control

Table of contents 1. Operator 1.1 Arithmetic oper...