The fastest way to remove duplicates from an array in javascript

PHPz
Release: 2023-05-16 09:40:37
Original
617 people have browsed it

As a scripting language, JavaScript is widely used in Web development and front-end UI design because it is easy to read and write. As a common data structure, arrays often play an important role in the use of JavaScript, such as traversing in loops, sorting, etc. When performing array operations, we often encounter duplication of array elements. At this time, we need to perform deduplication operations on the array.

So, how to deduplicate JavaScript arrays fastest?

Method 1: Use Set (ES6)

In native JavaScript, Set solves the problem of array deduplication very well. It can quickly complete deduplication for an unknown number of element collections. and sorting operations. The following is the code implementation using Set to remove duplicates:

let uniqueArray = Array.from(new Set(originalArray)); 
Copy after login

Or it can be written as

let uniqueArray= [...new Set(originalArray)]; 
Copy after login

Among them, the Array.from() method is used to convert the Set into an array, and [...] is The spread operator is a way to directly convert a Set into an array.

The deduplication method using Set has simple code and is more efficient when processing large-scale arrays. However, this method is not very compatible with IE browser, so it is recommended to check whether the browser supports Set before use.

Method 2: Use indexOf

Using the indexOf method can easily determine whether the array element exists, thereby realizing the array deduplication function. The following is the code for using indexOf to implement array deduplication:

let uniqueArray = [];
for(let i = 0; i < originalArray.length; i++){
    if(uniqueArray.indexOf(originalArray[i]) === -1){
        uniqueArray.push(originalArray[i]);
    }
}
Copy after login

In this implementation, we use the array uniqueArray to store unique elements, traverse the original array originalArray, and determine whether the elements in it already exist in the uniqueArray. If it does not exist, add it to uniqueArray.

Although the code size of this method is slightly complex, it is very fast on smaller arrays because it only needs to be looped once.

Method 3: Use the new ES6 method includes

ES6 has a new includes method, which can be used to check whether the array contains a certain element. The includes method is faster than indexOf when processing large arrays. many. The following is the code for using includes to implement array deduplication:

let uniqueArray = [];
for(let i = 0; i < originalArray.length; i++){
    if(!uniqueArray.includes(originalArray[i])){
        uniqueArray.push(originalArray[i]);
    }
}
Copy after login

Similar to the indexOf method, this implementation also uses a new array uniqueArray to store non-duplicate elements. This approach is readable and allows for fast deduplication in large arrays.

Conclusion

Among the above three methods, using Set is the simplest and fastest. But it should be noted that you need to check whether the browser is compatible with Set. The indexOf method is more readable, but only suitable for small arrays. Using ES6's includes method can handle large arrays, and it is also shorter and easier to read.

In actual projects, try to use different methods and choose the method that best suits you according to the actual situation.

The above is the detailed content of The fastest way to remove duplicates from an array in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!