// Uniqueness: Set vs Array
// Set automatically handles uniqueness
console.log([...set]); // [1, 2]
// Array requires manual checks
if (!arr.includes(1)) arr.push(1);
if (!arr.includes(2)) arr.push(2);
if (!arr.includes(1)) arr.push(1);
console.log(arr); // [1, 2]
// Performance: Membership testing
const largeSet = new Set(Array.from({ length: 10000 }, (_, i) => i));
console.timeEnd("Set has");
// Array is O(n) - slower for large arrays
const largeArr = Array.from({ length: 10000 }, (_, i) => i);
console.time("Array includes");
console.timeEnd("Array includes");
// Converting between Set and Array
// Array to Set (remove duplicates)
const numbers = [1, 2, 2, 3, 3, 3, 4, 5, 5];
const uniqueSet = new Set(numbers);
console.log([...uniqueSet]); // [1, 2, 3, 4, 5]
const mySet = new Set(["a", "b", "c"]);
const myArray = [...mySet];
console.log(myArray); // ['a', 'b', 'c']
// Array.from() also works
const anotherArray = Array.from(mySet);
console.log(anotherArray); // ['a', 'b', 'c']
// Practical: Removing duplicates from array
const withDuplicates = [1, 2, 3, 2, 4, 3, 5, 1, 6];
const withoutDuplicates = [...new Set(withDuplicates)];
console.log(withoutDuplicates); // [1, 2, 3, 4, 5, 6]
const fruitsArray = ["apple", "banana", "orange"];
console.log(fruitsArray[1]); // 'banana' (O(1))
const fruitsSet = new Set(["apple", "banana", "orange"]);
// console.log(fruitsSet[1]); // undefined (doesn't work)
console.log([...fruitsSet][1]); // 'banana' (convert first, O(n))
// Array methods vs Set methods
const arrayHas = fruitsArray.includes("banana"); // true
const setHas = fruitsSet.has("banana"); // true
// Array deletion (O(n) - needs to find and shift)
const deleteFromArray = (arr, value) => {
const index = arr.indexOf(value);
if (index !== -1) arr.splice(index, 1);
fruitsSet.delete("banana");
// When to use Array (index-based operations)
const playlist = ["Song 1", "Song 2", "Song 3"];
const currentSong = playlist[1]; // Access by index
playlist.splice(1, 0, "New Song"); // Insert at position
// When to use Set (unique collection)
const visitedUrls = new Set();
visitedUrls.add("/home");
visitedUrls.add("/about");
visitedUrls.add("/home"); // Duplicate, ignored
if (visitedUrls.has("/about")) {
console.log("Already visited about page");
// Hybrid approach: Use Set for uniqueness, convert to Array when needed
return this.set.delete(item);
return this.set.has(item);
return this.toArray().map(fn);
return this.toArray().filter(fn);
const uniqueList = new UniqueList();
uniqueList.add("apple").add("banana").add("apple");
console.log(uniqueList.toArray()); // ['apple', 'banana']
console.log(uniqueList.size); // 2
console.log(uniqueList.map((fruit) => fruit.toUpperCase())); // ['APPLE', 'BANANA']
// Set typically uses more memory than Array for the same number of elements
// because it maintains hash tables for O(1) lookups
// Use case example: Tag system
this.tags = new Set(); // No duplicate tags
this.tags.add(tag.toLowerCase());
this.tags.delete(tag.toLowerCase());
return this.tags.has(tag.toLowerCase());
return [...this.tags].sort();
const tags = new TagManager();
tags.addTag("JavaScript");
tags.addTag("JavaScript"); // Duplicate ignored
console.log(tags.getTags()); // ['javascript', 'node.js', 'react']
console.log(tags.hasTag("React")); // true
console.log(tags.getTagCount()); // 3