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
Selected Reading
Removing duplicate objects from array in JavaScript
Removing duplicate objects from arrays is a common requirement in JavaScript applications. Let's explore different approaches to eliminate duplicate objects while preserving the original data structure.
The Problem
Consider an array containing duplicate objects:
const arr = [
{"title": "Assistant"},
{"month": "July"},
{"event": "Holiday"},
{"title": "Assistant"}
];
console.log("Original array:", arr);
Original array: [
{ title: 'Assistant' },
{ month: 'July' },
{ event: 'Holiday' },
{ title: 'Assistant' }
]
Using JSON.stringify() with Filter
The most straightforward approach uses JSON.stringify() to convert objects to strings for comparison:
const arr = [
{"title": "Assistant"},
{"month": "July"},
{"event": "Holiday"},
{"title": "Assistant"}
];
const removeDuplicates = (array) => {
const seen = new Set();
return array.filter(obj => {
const str = JSON.stringify(obj);
if (seen.has(str)) {
return false;
}
seen.add(str);
return true;
});
};
const uniqueArray = removeDuplicates(arr);
console.log("Unique objects:", uniqueArray);
Unique objects: [
{ title: 'Assistant' },
{ month: 'July' },
{ event: 'Holiday' }
]
In-Place Removal with Splice
For modifying the original array directly:
const arr = [
{"title": "Assistant"},
{"month": "July"},
{"event": "Holiday"},
{"title": "Assistant"}
];
const removeDuplicateInPlace = (array) => {
const map = {};
for(let i = 0; i
Modified original array: [
{ title: 'Assistant' },
{ month: 'July' },
{ event: 'Holiday' }
]
Using Map for Better Performance
For larger datasets, using Map provides better performance than plain objects:
const arr = [
{"title": "Assistant"},
{"month": "July"},
{"event": "Holiday"},
{"title": "Assistant"},
{"month": "July"}
];
const removeDuplicatesWithMap = (array) => {
const seen = new Map();
return array.filter(obj => {
const key = JSON.stringify(obj);
if (seen.has(key)) {
return false;
}
seen.set(key, true);
return true;
});
};
const result = removeDuplicatesWithMap(arr);
console.log("Result with Map:", result);
Result with Map: [
{ title: 'Assistant' },
{ month: 'July' },
{ event: 'Holiday' }
]
Comparison of Methods
| Method | Modifies Original | Performance | Memory Usage |
|---|---|---|---|
| Filter with Set | No | Good | Higher |
| Splice In-Place | Yes | Poor (O(n²)) | Lower |
| Filter with Map | No | Best | Higher |
Key Points
-
JSON.stringify()converts objects to strings for comparison - Filter methods create new arrays without modifying the original
- In-place methods modify the original array directly
- Use
SetorMapfor better performance with large datasets
Conclusion
The filter approach with Set or Map is generally recommended as it's clean, functional, and performant. Use in-place methods only when memory conservation is critical.
Advertisements
