Open In App

JavaScript set Interview Questions

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A Set in JavaScript is a collection of unique values that maintains insertion order and removes duplicates automatically. It supports efficient operations like adding, deleting, and checking values, making it ideal for tasks involving uniqueness or deduplication. Unlike arrays, Set does not allow duplicate elements and provides methods like add(), delete(), and has() for easy manipulation.

We are going to discuss some Interview Questions related to set.

1. Explain the Set object in JavaScript.

A Set is a collection of unique values. Unlike arrays, duplicate values are not allowed. Even though 3 is repeated, Set stores only unique values.

JavaScript
const mySet = new Set([1, 2, 3, 3, 4]);
console.log(mySet);

Output
Set(4) { 1, 2, 3, 4 }

2. Demonstrate how to add elements to a Set.

JavaScript
const mySet = new Set();
mySet.add(10);
mySet.add(20);
mySet.add(10); // Duplicate
console.log(mySet);

Output
Set(2) { 10, 20 }

The .add() method adds elements to the Set, but duplicates are ignored.

3. Show how to remove a specific element from a Set.

JavaScript
const mySet = new Set([1, 2, 3]);
mySet.delete(2);
console.log(mySet);

Output
Set(2) { 1, 3 }

Explanation: The .delete() method removes the specified value from the Set.

4. What is the difference between Set and Array?

FeatureSetArray
DuplicatesNot allowedAllowed
OrderInsertion orderIndex-based order
Methods.add().has().delete().push().indexOf()

5. Write code to check if a value exists in a Set.

JavaScript
const mySet = new Set([1, 2, 3, 4]);
console.log(mySet.has(3)); // true
console.log(mySet.has(5)); // false

Output
true
false

6. Write code to convert a Set to an array.

JavaScript
const mySet = new Set([1, 2, 3]);
const myArray = Array.from(mySet);
console.log(myArray);

Output
[ 1, 2, 3 ]

7. Write code to compute the union of two sets.

JavaScript
const setA = new Set([1, 2, 3]);
const setB = new Set([3, 4, 5]);
const union = new Set([...setA, ...setB]);
console.log(union);

Output
Set(5) { 1, 2, 3, 4, 5 }

8. Write code to find common elements between two sets.

JavaScript
const setA = new Set([1, 2, 3]);
const setB = new Set([2, 3, 4]);
const intersection = new Set([...setA].filter(x => setB.has(x)));
console.log(intersection);

Output
Set(2) { 2, 3 }

9. Write code to iterate over a Set.

JavaScript
const mySet = new Set(['a', 'b', 'c']);
for (const value of mySet) {
    console.log(value);
}

Output
a
b
c

10. What will be the output?

JavaScript
const mySet = new Set();
mySet.add({ id: 1 });
mySet.add({ id: 1 }); 
console.log(mySet.size);

Output
2

Explanation: A Set can store objects, but it treats different object references as unique.

11. Write code to clear all elements from a Set.

JavaScript
const mySet = new Set([1, 2, 3]);
mySet.clear();
console.log(mySet);

Output
Set(0) {}

12. What will be the output?

JavaScript
const mySet = new Set();
mySet.add(1);
mySet.add("1");
mySet.add(1);
console.log(mySet.size);

Explanation: The Set treats 1 (number) and "1" (string) as different values. Although 1 is added twice, it is stored only once.

13. What will be the output?

JavaScript
const mySet = new Set(["apple", "banana", "apple", "cherry"]);
console.log([...mySet]);

14. What will be the output?

JavaScript
const mySet = new Set([NaN, NaN, "NaN"]);
console.log(mySet);

Explanation: In a Set, NaN is treated as equal to itself, even though NaN === NaN is false. "NaN" (string) is distinct from NaN.

15. What will be the output?

JavaScript
const mySet = new Set();
const obj = { id: 1 };
mySet.add(obj);
mySet.add({ id: 1 });
console.log(mySet.size);

Explanation: Each object has a unique reference. Even though both objects have the same properties, they are different objects.


Next Article
Article Tags :

Similar Reads