The fastest algorithm to remove duplicates from JS arrays

php中世界最好的语言
Release: 2018-04-14 16:14:38
Original
3367 people have browsed it

This time I will bring you the fastest algorithm for deduplication in JSarray, what are thenotesfor deduplication in JS array, the following is a practical case, let’s take a look take a look.

In JS, we often encounter the need to remove duplicate data inarrays. Here we introduce four algorithms to achieve the function of deduplication in JS arrays.

1. The fastest algorithm:ObjectKey-value pair method

Implementation idea:Create a new js object and a new array. When traversing the incoming array, determine whether the value is the key of the js object. If not, add the key to the objectand put the new key in it. array.

//注意点: 判断 是否为js对象键时,会自动对传入的键执行“toString()”,不同的键可能会被误认为一样;例如: a[1]、a["1"] 。解决上述问题还是得调用“indexOf”。 //速度最快, 占空间最多(空间换时间) function unique(array){ var n = {}, r = [], len = array.length, val, type; for (var i = 0; i 

operation result:

The fastest algorithm to remove duplicates from JS arrays

2. The most ingenious algorithm: optimizationTraverse the arraymethod

Implementation idea: Get the rightmost value without duplication and put it into a new array. (When duplicate values are detected, the current loop is terminated and the next round of judgment of the top-level loop is entered)

function unique1(array){ var r = []; for(var i = 0, l = array.length; i

operation result:

The fastest algorithm to remove duplicates from JS arrays

3. Algorithm: Sorted Adjacent Removal Method

Implementation idea: Sort the incoming array . After sorting, the same values are adjacent, and then when traversing, only add values that are not duplicates of the previous value to the new array.

//将相同的值相邻,然后遍历去除重复值 function unique2(array){ array.sort(); var re=[array[0]]; for(var i = 1; i 

operation result:

The fastest algorithm to remove duplicates from JS arrays

4. Algorithm: Array subscript judgment method

Implementation idea: If the first occurrence of the i-th item in the current array is not i, then it means that the i-th item is repeated and ignored. Otherwise, store the result array

function unique3(array){ var n = [array[0]]; //结果数组 //从第二项开始遍历 for(var i = 1; i 

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

JS method to remove duplicate items in an array

How to use bootstrap responsive navigation bar template

vue.js method of operating array data

Copy after login

The above is the detailed content of The fastest algorithm to remove duplicates from JS arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
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!