Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
1. Introduction to collection
1. What is a set?
- A kind of
Unordered and unique
Data structure of - ES6 has a collection called
Set
- Common operations of set: de-duplication, determine whether an element is in the set, find the intersection…
# 2. De-duplicate, determine whether the element is in the set, and find the intersection
/ / to heavy
const arr = [1.1.2.2];
const arr2 = [...new Set(arr)];
// Determine if the element is in the set
const set = new Set(arr);
const has = set.has(3);
/ / intersection
const set2 = new Set([2.3]);
const set3 = new Set([...set].filter(item= > set2.has(item)));
Copy the code
2. LeetCode: 349. Intersection of two arrays
intersection-of-two-arrays
Leetcode-cn.com/problems/in…
2.1 Steps to solve the problem
- Deduplicate nums1 with sets
- Iterate through nums1 to filter out values that nums 2 also contains
/ * * *@param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}* /
var intersection = function(nums1, nums2) {
return [...new Set(nums1)].filter(item= > nums2.includes(item))
};
Copy the code
Time complexity: Om x n Space complexity: Om
3. Front-end and Collections: Use ES6 Set
- Use Set objects, new, add, delete, has, size
- Iterative Set: a variety of iterative methods, Set and Array interconversion, intersection \ difference Set
3.1 createnew Set()
/ / 1. To create
let mySet = new Set(a);Copy the code
3.2 add() : Adds a value and returns the Set structure itself
let mySet = new Set(a); mySet.add(1);
// Add - Add multiple times and retain only one time
mySet.add(5);
mySet.add(5);
// add - add string
mySet.add('some text');
// Add - Add objects
let o = { a: 1.b: 2 };
mySet.add(o);
{a: 1, b: 2}}
mySet.add({ a: 1.b: 2 });
Copy the code
3.3 HAS () : Checks whether the value exists and returns a Boolean
let mySet = new Set(a);let o = { a: 1.b: 2 };
mySet.add(o);
const has = mySet.has(o);
Copy the code
3.4 delate() : Deletes the value and returns a Boolean indicating whether the deletion was successful
const mySet = new Set([5]);
mySet.delete(5);
Copy the code
3.5 Iterate keys(), values() and. Entries ()
let mySet = new Set(a); mySet.add(1);
mySet.add(5);
mySet.add(5);
mySet.add('some text');
let o = { a: 1.b: 2 };
mySet.add(o);
mySet.add({ a: 1.b: 2 });
for(let [key, value] of mySet.entries()) console.log(key, value);
Copy the code
3.6 Set Array Transfer
const myArr = Array.from(mySet);
const mySet2 = new Set([1.2.3.4]);
Copy the code
3.7 o intersection
const intersection = new Set([...mySet].filter(x= > mySet2.has(x)));
Copy the code
3.8 difference set
const difference = new Set([...mySet].filter(x= >! mySet2.has(x)));Copy the code
3.9 clear() : Clears all values. No return value
let set = new Set(a); set.add(1).add(2);
set.clear();
console.log(set,[...set]); // Set(0){} []
Copy the code
4. To summarize
The main technical points
- A set is a kind of
Unordered and unique
Data structure of - ES6 has a collection called
Set
- Common operations of set: de-duplication, determine whether an element is in the set, find the intersection…