Home > Web Front-end > Front-end Q&A > How to determine if two arrays have duplicate elements in javascript

How to determine if two arrays have duplicate elements in javascript

青灯夜游
Release: 2022-09-02 17:40:52
Original
9003 people have browsed it

Judgment steps: 1. Use the "new Set (array)" statement to convert both arrays into set collection types; 2. Use "new Set([...set 1].filter(x = > Set 2.has(x)))" statement to obtain the intersection of two sets, it will return a new set containing all intersection elements; 3. Use the "Array.from (intersection set)" statement to convert the set into an array type ; 4. Use the "Intersection array == []" statement to determine whether the intersection array is an empty array. If so, the two arrays have no duplicate elements.

How to determine if two arrays have duplicate elements in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

To determine whether two arrays have duplicate elements, in other words, to determine whether two arrays intersect.

In JavaScript, you can use the has() method of the set object in conjunction with the filter() of the array to make a judgment.

Implementation steps:

Step 1: Convert both arrays to set collection types

let a=[1, 2, 3];
let b=[3, 5, 2];
newA = new Set(a);
newB = new Set(b);
Copy after login

Step 2: Use the has() and filter() functions to obtain the intersection of the two sets

let a=[1, 2, 3];
let b=[3, 5, 2];
newA = new Set(a);
newB = new Set(b); 
let intersectionSet = new Set([...newA].filter(x => newB.has(x)));
console.log(intersectionSet);
Copy after login

How to determine if two arrays have duplicate elements in javascript

It can be seen that at this time, the intersection elements are included in a set Returned from the collection.

Step 3: Use the Array.from method to convert the collection into an array type

The Array.from method is used to convert two types of objects into real arrays: similar to arrays Objects (array-like objects) and iterable (iterable) objects (including ES6's new data structures Set and Map).

let a=[1, 2, 3];
let b=[3, 5, 2];
newA = new Set(a);
newB = new Set(b); 
let intersectionSet = new Set([...newA].filter(x => newB.has(x)));
console.log(intersectionSet);

let arr = Array.from(intersectionSet);
console.log(arr);
Copy after login

How to determine if two arrays have duplicate elements in javascript

Step 4: Determine whether the intersection array is an empty array

  • is an empty array , then the two arrays have no duplicate elements

  • is not an empty array, then the two arrays have duplicate elements

if(arr==[]){
	console.log("两个数组没有重复元素");
}else{
	console.log("两个数组有重复元素");
}
Copy after login

How to determine if two arrays have duplicate elements in javascript

(Learning video sharing: web front-end)

The above is the detailed content of How to determine if two arrays have duplicate elements in javascript. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template