Working with Sets Flashcards

(17 cards)

1
Q

How do you create a new, empty Set in JavaScript?

A

const mySet = new Set();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create a Set and initialize it with values from an existing array?

A

const myArray = [1, 2, 2, 3]; const mySet = new Set(myArray); // duplicates removed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you add a new element to a Set?

A

mySet.add(value); // e.g., mySet.add(5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens if you try to add a duplicate value to a Set?

A

Duplicate values are ignored — Sets only store unique items.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you remove an element from a Set?

A

mySet.delete(value); // e.g., mySet.delete(2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the return value of the delete() method in Sets?

A

true if removed successfully; false if element not found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you check if a Set contains a specific value?

A

mySet.has(value); // e.g., mySet.has(7);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the return value of the has() method in Sets?

A

Boolean — true if the value exists, false otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you remove all elements from a Set?

A

mySet.clear();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you loop through all the values in a Set?

A

for (const value of mySet) { … } // or mySet.forEach(v => { … });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Does a Set have indexes like an array?

A

No — Sets have no indexes; you can’t access by position.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you convert a Set back into an array?

A

const myArray = […mySet];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you efficiently remove duplicates from an array using Sets?

A

const uniqueArray = […new Set(myArrayWithDuplicates)];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Are Sets guaranteed to maintain insertion order?

A

Yes — Sets preserve the order elements were added.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you perform a union of two Sets (all unique elements)?

A

function setUnion(setA, setB) {
const unionSet = new Set(setA);
for (const elem of setB) unionSet.add(elem);
return unionSet;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you perform an intersection of two Sets (common elements)?

A

function setIntersection(setA, setB) {
const intersectionSet = new Set();
for (const elem of setB)
if (setA.has(elem)) intersectionSet.add(elem);
return intersectionSet;
}

17
Q

How do you perform a difference of two Sets (A minus B)?

A

function setDifference(setA, setB) {
const differenceSet = new Set(setA);
for (const elem of setB) differenceSet.delete(elem);
return differenceSet;
}