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 "Intersection Array ==[]" statement determines whether the intersection array is an empty array. If so, the two arrays have no duplicate elements."/> 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 "Intersection Array ==[]" statement determines whether the intersection array is an empty array. If so, the two arrays have no duplicate elements.">
Home > Article > Web Front-end > How to determine if two arrays have duplicate elements in javascript
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.
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);
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);
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);
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("两个数组有重复元素"); }
(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!