search
HomeWeb Front-endJS TutorialSummary of JS array deduplication methods

Summary of JS array deduplication methods

Mar 17, 2018 pm 04:36 PM
javascriptSummarizemethod

This article mainly shares with you a summary of JS array deduplication methods. There are seven methods in total. I hope it can help everyone.

The easiest way:

##?

1

2

3

4

##5

6

7

8

9

10

11

12

13

14

15

16

17

18


var arr=[2,8,5,0,5,2,6,7,2];
function unique1(arr){
  var hash=[];
  for (var i = 0; i      if(hash.indexOf(arr[i])==-1){
      hash.push(arr[i]);
     }
  }
  return hash;

}


Method 1:

Double-layer loop, outer loop element, inner loop comparison value

If there is If the values ​​are the same, they will be skipped. If they are not the same, they will be pushed into the array.

?

Method 2: Use splice to operate directly on the original array

Double-layer loop, outer loop element, inner loop comparison value

When the values ​​are the same, delete this value

Note: After deleting the element, you need to reduce the length of the array by 1.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18


Array.prototype.distinct = function(){

 var arr = this,

  result = [],

  i,

  j,

  len = arr.length;

 for(i = 0; i

  for(j = i + 1; j

   if(arr[i] === arr[j]){

    j = ++i;

   }

  }

  result.push(arr[i]);

 }

return result;

##}

var arra = [1,2,3,4,4,1,1,2,1,1,1];

arra.distinct(); //Return [3,4,2,1]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19


Array.prototype.distinct = function (){

 var arr = this,

  i,

  j,

  len = arr.length;

 for(i = 0; i

  for(j = i + 1; j

   if(arr[i] == arr[j]){

    arr.splice(j,1);

    len--;

    j--;

#}

#}

 }

 return arr;

};

var a = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,];

var b = a.distinct();

console.log(b.toString()); //1,2,3,4,5,6,56

Advantages: simple and easy to understand

Disadvantages: high memory usage and slow speed

Method 3: Use the properties of objects that cannot be the same to deduplicate

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17


Array.prototype.distinct = function (){

 var arr = this,

  i,

  obj = {},

  result = [],

  len = arr.length;

 for(i = 0; i

if(!obj[arr[i]]){ //If it can be found, it proves that the array elements are repeated

obj[arr[i]] = 1;

result.push(arr[ i]);

  }

 }

 return result;

};

var a = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,];

var b = a.distinct();

console.log(b.toString()); //1,2,3,4,5,6,56

Method 4: Recursive deduplication of arrays

Use the recursive idea

Sort first, and then compare from the end, if you encounter the same , then delete


?

##1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20


Array.prototype.distinct = function (){

 var arr = this,

  len = arr.length;

arr.sort(function(a,b){ //Sort the array to facilitate comparison

return a - b;

})

 function loop(index){

  if(index >= 1){

   if(arr[index] === arr[index-1]){

arr.splice(index,1);

}

loop(index - 1); //Recursive loop function to remove duplicates

}

 }

 loop(len-1);

 return arr;

};

var a = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,56,45,56];

var b = a.distinct();

console.log(b.toString());  //1,2,3,4,5,6,45,56

Method Five: Using indexOf and forEach

?

##1

2

3

4

5

6

7

8

9

10

11

12

13

14

15


Array.prototype.distinct = function (){

 var arr = this,

  result = [],

  len = arr.length;

arr.forEach(function(v, i ,arr){ //Use map and filter here The method can also be implemented

var bool = arr.indexOf(v,i+1); // Start looking for duplicates from the next index value of the incoming parameter

  if(bool === -1){

   result.push(v);

  }

 })

 return result;

};

var a = [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,3,2,3,3,2,2,1,23,1,23,2,3,2,3,2,3];

var b = a.distinct();

console.log(b.toString()); //1,23,2,3

Method 6: Use ES6's set

Set data structure, which is similar to an array, and the values ​​of its members are all unique.

Use Array.from to convert the Set structure into an array

?

##1

2

3

4


function dedupe(array){

 return Array.from(new Set(array));

}

dedupe([1,1,2,3]) //[1,2,3]

#

The expansion operator (...) uses the for...of loop internally

?

1

2

3


#let let arr = [1,2,3,3 ];

let resultarr = [...new Set(arr)];

console.log(resultarr); //[1,2,3]

The following is a supplementary introduction to the method of merging arrays and removing duplicates

##1. concat() method

Idea: The concat() method combines the incoming array or non-array value with the original array to form a new array and returns it. This method will generate a new array.


##1

2

3

4

5

function concatArr(arr1, arr2){

##var arr = arr1.concat (arr2);

arr = unique1(arr);//Refer to any of the above deduplication methods

  return arr;

}

2. Array.prototype.push.apply()

Idea: The advantage of this method is that it does not generate a new array.

?

##1

2

3

4

5

6

7

8

9

10


var a = [1, 2, 3];

var b = [4, 5, 6];

Array.prototype.push.apply(a, b);//a=[1,2,3,4,5,6]

//等效于:a.push.apply(a, b);

//Also equivalent to [].push.apply(a, b);

function concatArray(arr1,arr2){

Array.prototype.push.apply(arr1, arr2);

arr1 = unique1(arr1);

return arr1;

##Related recommendations:

Detailed explanation of array deduplication examples in js

Six ways to deduplicate JS arrays

How to deduplicate JavaScript arrays Ways to share

The above is the detailed content of Summary of JS array deduplication methods. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!