In-depth study of JavaScript array deduplication problem

In-depth study of JavaScript array deduplication problem

Preface πŸ‘€

Array deduplication should be a very common problem. Since it is common, we should learn about it!

Lest I be embarrassed if I don't know how to do it~ Hehe

Start researching πŸ±β€πŸ

Original 🧢

To deduplicate an array, my initial idea was this: define a new array, complete two layers of for loops, if the data appears for the first time, push it to the new array, if it is repeated, break it, use the value of j to be equal to the length of res to determine that the data is unique, and finally return the new array.

var arr = [1,1,2,3,4,5,6,7,4,3,'1',8,'3','1','3','66']

function unique(arr){
	var res = []
	for(var i = 0; i < arr.length; i++){
		for(var j = 0; j < res.length; j ++){
			if(arr[i] === res[j]){
				break
			}
		}
		// If the data appears for the first time, then after executing the above for statement, the value of j should be equal to the length of res if(j === res.length){
			res.push(arr[i])
		}
	}
	return res;
}

console.log(unique(arr));

Optimizing the original method using indexOf ✍

Let's first take a brief look at indexOf:

The indexOf(item,start) method returns the position of a specified element in an array.

This method will search the array from beginning to end to see if it contains the corresponding element. The search starts at the start of the array or at the beginning of the array (when the start parameter is not specified). If an item is found, the position of the first occurrence of item is returned. The starting position has an index of 0.

If the specified element is not found in the array, -1 is returned.

Seeing this, everyone understands what we are taking advantage of, right? Yes, it is the bold sentence: If the specified element is not found in the array, -1 is returned .

var arr = [1,1,2,3,4,5,6,7,4,3,'1',8,'3','1','3','66']

function unique(arr){
	var res = []
	for(var i = 0; i < arr.length; i++){
		if (res.indexOf(arr[i]) === -1) {
			res.push(arr[i])
		}
	}
	return res;
}

console.log(unique(arr));

Optimize again, filter method πŸŽ‰

As the name implies, filter means filtering. This method creates a new array. The elements in the new array are obtained by checking all elements that meet the conditions in the specified array.

Idea: Use filter instead of a layer of loop in conjunction with indexOf to achieve the filtering effect and directly return the deduplicated array.

var arr = [1,1,2,3,4,5,6,7,4,3,'1',8,'3','1','3','66']

function unique(arr){
	var res = arr.filter(function(item,index,arr){
		return arr.indexOf(item) === index
	})
	return res;
}
console.log(unique(arr));

Change of thinking? Become an ordered array ✨

I wonder if those of you who have been playing Lecture Hall for a few days have had this feeling. When you see an array in a question, your eyes immediately glance forward to see if it is an ordered array or an unordered array.

Back to this question, we make the array to be deduplicated ordered, and the repeated data must be next to each other. Use a variable to store the previous element value, and then loop to determine whether the current value is the same as the previous element value. If not, add it to res.

var arr = [1,1,2,3,4,5,6,7,4,3,'1',8,'3','1','3','66']

function unique(arr){
	var res = []
	var pre
	arr = arr.sort()
	for(var i = 0; i < arr.length; i++){
		if(!i || pre !== arr[i]){
			res.push(arr[i])
		}
		pre = arr[i]
	}
	return res;
}

console.log(unique(arr));

Optimize again, filter🧨

I just realized that filter can also rewrite the sorting here to make it more concise. Let's look at the code directly:

var arr = [1,1,2,3,4,5,6,7,4,3,'1',8,'3','1','3','66']

function unique(arr){
	var res = arr.sort().filter(function(item,index,arr){
		return !index || item !== arr[index - 1]
	})
	return res;
}

console.log(unique(arr));

ES6, Set is coming 🧸

ES6 brings us many benefits, among which map and set are particularly outstanding.

Map objects store key-value pairs. Any value (object or primitive) can be used as a key or a value.

The Set object allows you to store unique values ​​of any type, whether primitive values ​​or object references.

So we can use this feature of Set to perform deduplication processing.

var arr = [1,1,2,3,4,5,6,7,4,3,'1',8,'3','1','3','66']

function unique(arr){
	return Array.from(new Set(arr))
}

console.log(unique(arr));

Note: Set is an object, so it must be converted into an array for return.

If you understand destructuring assignment, you can simplify it a little bit more🧡

var arr = [1,1,2,3,4,5,6,7,4,3,'1',8,'3','1','3','66']

function unique(arr){
	return [...new Set(arr)]
}

console.log(unique(arr));

If you want to know more about destructuring assignment, you can also read this first: Understanding and Application of Destructuring Operators

Previous study, recorded notes 🎨

Keep on being great (arrow functions) πŸ†

var arr = [1,1,2,3,4,5,6,7,4,3,'1',8,'3','1','3','66']
var unique = (arr) => [...new Set(arr)]
console.log(unique(arr));

Finally πŸ“–

From several lines of code at the beginning to one line of code at the end using arrow functions, it is enough to show that only by continuous learning can we write more elegant and concise code.

This is the end of this article about JavaScript array deduplication. For more relevant JavaScript array deduplication content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JS array deduplication details
  • Detailed discussion of several methods for deduplicating JavaScript arrays
  • JavaScript array deduplication solution
  • js implements array flattening
  • JavaScript data flattening detailed explanation
  • JavaScript Interview: How to implement array flattening method
  • 5 JavaScript Ways to Flatten Arrays
  • Introduction to JavaScript array deduplication and flattening functions

<<:  Detailed explanation of MySQL's FreeList mechanism

>>:  Analyze the role of rel="nofollow" in HTML and the use of rel attribute

Recommend

How to unify the character set on an existing mysql database

Preface In the database, some data tables and dat...

Centos6.9 installation Mysql5.7.18 step record

Installation sequence rpm -ivh mysql-community-co...

An article to understand the usage of typeof in js

Table of contents Base Return Type String and Boo...

Let's talk about the issue of passing parameters to React onClick

Background In a list like the one below, clicking...

MAC+PyCharm+Flask+Vue.js build system

Table of contents Configure node.js+nvm+npm npm s...

MySQL learning notes: data engine

View the engines supported by the current databas...

Docker installs redis 5.0.7 and mounts external configuration and data issues

Redis is an open source NoSQL database written in...

A brief analysis of different ways to configure static IP addresses in RHEL8

While working on a Linux server, assigning static...

K3s Getting Started Guide - Detailed Tutorial on Running K3s in Docker

What is k3d? k3d is a small program for running a...

FlashFXP ftp client software registration cracking method

The download address of FlashFXP is: https://www....

Tutorial on installing VMWare15.5 under Linux

To install VMWare under Linux, you need to downlo...

3 functions of toString method in js

Table of contents 1. Three functions of toString ...