Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Arrays vs Set in JavaScript.
In this article, we will learn about the difference between an array and a set in JavaScript. Arrays are used to store ordered collections of elements, whereas Sets store only unique values in an unordered collection.
What is an Array?
An Array is an ordered, indexed collection of values in JavaScript. It allows duplicate values and provides various built-in methods to manipulate elements.
Syntax
let numbers = [1, 2, 3, 4, 5];
What is a Set?
A Set is an unordered collection of unique values in JavaScript. Unlike arrays, a Set automatically removes duplicate values and maintains insertion order for iteration.
Syntax
let numbers = new Set([1, 2, 3, 4, 5]);
The Set data type was introduced in ES6. The key difference is that while an array can have duplicate values, a Set cannot. Elements in arrays can be accessed using an index, but Set elements are accessed through iteration methods.
Key Differences
| Feature | Array | Set |
|---|---|---|
| Duplicates | Allows duplicates | Only stores unique values |
| Ordering | Maintains insertion order | Maintains insertion order for iteration |
| Indexing | Direct index access (arr[0]) | No direct indexing |
| Performance | Slower for checking existence (includes()) | Faster lookup using has() |
| Modification | Can modify elements directly | Elements can only be added or removed |
| Iteration | for, forEach, map, etc. | forEach, for...of, spread operator |
Example: Array vs Set Comparison
// Array with duplicates
let arr = [1, 2, 3, 2, 4, 3, 5];
console.log("Array:", arr);
console.log("Array length:", arr.length);
// Set removes duplicates automatically
let set = new Set(arr);
console.log("Set:", set);
console.log("Set size:", set.size);
// Accessing elements
console.log("Array element at index 1:", arr[1]);
console.log("Set has value 2:", set.has(2));
// Converting Set back to Array
let uniqueArray = [...set];
console.log("Unique array:", uniqueArray);
Array: [ 1, 2, 3, 2, 4, 3, 5 ]
Array length: 7
Set: Set(5) { 1, 2, 3, 4, 5 }
Set size: 5
Array element at index 1: 2
Set has value 2: true
Unique array: [ 1, 2, 3, 4, 5 ]
Performance Comparison
let largeArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let largeSet = new Set(largeArray);
// Checking if element exists
console.time("Array includes");
console.log("Array includes 5:", largeArray.includes(5));
console.timeEnd("Array includes");
console.time("Set has");
console.log("Set has 5:", largeSet.has(5));
console.timeEnd("Set has");
Array includes 5: true Array includes: 0.123ms Set has 5: true Set has: 0.045ms
Common Use Cases
Use Arrays when:
- You need indexed access to elements
- Order matters and you need to maintain duplicates
- You need array methods like map(), filter(), reduce()
Use Sets when:
- You need to ensure unique values
- Fast lookup/existence checking is important
- You want to remove duplicates from an array
Conclusion
Arrays are ideal for ordered collections with duplicates and indexed access, while Sets excel at storing unique values with fast lookup performance. Choose based on whether you need duplicates, indexing, or uniqueness in your data structure.
