set in JavaScript
1. Creating an empty set
const sett = new Set();
console.log(sett);
output:
Set(0) {}
2. Creating set from array ?
const language = [
"English",
"Finnish",
"English",
"French",
"Spanish",
"English",
"French"
];
const setOfLanguages = new Set(language);
console.log(setOfLanguages);
output:
Set(4) { 'English', 'Finnish', 'French', 'Spanish' }
3. Set is an iterable object and we can iterate through each elements.
const languages = [
'English',
'Finnish',
'English',
'French',
'Spanish',
'English',
'French',
]
const setOfLanguages = new Set(languages)
for (const language of setOfLanguages) {
console.log(language)
}
output:
English
Finnish
French
Spanish
Set(0) {}
4. Adding an element to a set
const companies = new Set();
companies.add('Google');
console.log(companies);
output:
Set(1) { 'Google' }
5. Deleting an element a set
const arr = ['Apple',"Microsoft","Oracle"];
const arrToSet = new Set(arr);
console.log(arrToSet.delete("Microsoft"));
console.log(arrToSet);
output:
true
Set(2) { 'Apple', 'Oracle' }
6. Checking an element in the set.
const arr = ['Apple',"Microsoft","Oracle"];
const arrToSet = new Set(arr);
console.log(arrToSet.has('Apple'));
output: true
7. clearing set
const arr = ['Apple',"Microsoft","Oracle"];
const arrToSet = new Set(arr);
arrToSet.clear();
console.log(arrToSet);
output:
Set(0) {}
8. count unique item in an array using set.
const arr = [1,2,3,4,5,6,2,4,5];
const arrSet = new Set(arr);
console.log(arrSet);
output:
Set(6) { 1, 2, 3, 4, 5, 6 }
9. Union of sets
To find a union to two sets can be achieved using spread operator. Lets find the union of set A and set B (A U B)
let a = [1, 2, 3, 4, 5]
let b = [3, 4, 5, 6]
let c = [...a, ...b]
let A = new Set(a)
let B = new Set(b)
let C = new Set(c)
console.log(C)
output:
Set(6) { 1, 2, 3, 4, 5, 6 }
10. Intersection of sets
To find an intersection of two sets can be achieved using filter. Lets find the intersection of set A and set B (A ∩ B)
let a = [1, 2, 3, 4, 5]
let b = [3, 4, 5, 6]
let A = new Set(a)
let B = new Set(b)
let c = a.filter((num) => B.has(num))
let C = new Set(c)
console.log(C)
output:
Set(3) { 3, 4, 5 }
11. Difference of sets
To find an the difference between two sets can be achieved using filter. Lets find the different of set A and set B (A \ B)
let a = [1, 2, 3, 4, 5]
let b = [3, 4, 5, 6]
let A = new Set(a)
let B = new Set(b)
let c = a.filter((num) => !B.has(num))
let C = new Set(c)
console.log(C)
output:
Set(2) { 1, 2 }
--------------------------------------------------------------------------------------------------------------
Map
1. Creating an empty Map
const map = new Map()
console.log(map)
output:
Map(0) {}
2. Creating a Map from array
countries = [
["Japan","Iran"],
["Honkong","Turkey"],
["Dubai","German"],
["England","United States"]
];
const map = new Map(countries);
console.log(map);
output:
Map(4) {
'Japan' => 'Iran',
'Honkong' => 'Turkey',
'Dubai' => 'German',
'England' => 'United States'
}
3. Adding values to the Map
const countries = new Map();
countries.set("London","Paris");;
countries.set("Brazil","French");
console.log(countries);
output:
Map(2) { 'London' => 'Paris', 'Brazil' => 'French' }
4. Getting a value from Map
const countries = new Map();
countries.set("London","Paris");;
countries.set("Brazil","French");
console.log(countries.get("London"));
output:
Paris
5. Checking key in Map
const countries = new Map();
countries.set("London","Paris");;
countries.set("Brazil","French");
console.log(countries.has("Brazil"));
output: true
6. Getting all values from map using loop
const countries = new Map();
countries.set("London","Paris");;
countries.set("Brazil","French");
for(const val of countries){
console.log(val);
}
output:
[ 'London', 'Paris' ]
[ 'Brazil', 'French' ]