Get list of Duplicate Objects in an Array of Objects
Finding duplicates in an array of objects is a common task in JavaScript, especially when dealing with data manipulation and validation. This will guide you through the process of identifying duplicate objects in an array using various techniques.
Understanding the Problem
Let's assume we have an array of objects where each object has a set of key-value pairs. Our goal is to find all duplicate objects based on a specific key or a combination of keys. For simplicity, we'll use an array of user objects with ID, name, and email properties.
Example
const users = [
{ id: 1, name: 'Amit Kumar', email: 'amit@example.com' },
{ id: 2, name: 'Sumit Kumar', email: 'sumit@example.com' },
{ id: 3, name: 'Ami Kumar', email: 'amit@example.com' },
{ id: 4, name: 'Raj Kumar', email: 'raj@example.com' },
{ id: 5, name: 'Amit Kumar', email: 'amit@example.com' }
];
In the above array, the objects with id 1, 3, and 5 are duplicates based on the name and email properties.
Table of Content
1. Using a Hash Map
A simple approach is to use a hash map (or object) to keep track of occurrences of each object. This method works well for finding duplicates based on one or multiple properties.
Steps to Implement using a Hash Map
- Create a Hash Map
- Iterate Over the Array
- Store and Check for Duplicates
const users = [
{ id: 1, name: 'Amit Kumar', email: 'amit@example.com' },
{ id: 2, name: 'Sumit Kumar', email: 'sumit@example.com' },
{ id: 3, name: 'Ami Kumar', email: 'amit@example.com' },
{ id: 4, name: 'Raj Kumar', email: 'raj@example.com' },
{ id: 5, name: 'Amit Kumar', email: 'amit@example.com' }
];
function getDuplicates(arr, key) {
const map = {};
const duplicates = [];
arr.forEach(item => {
const keyValue = item[key];
if (map[keyValue]) {
duplicates.push(item);
} else {
map[keyValue] = true;
}
});
return duplicates;
}
const duplicateUsersByEmail = getDuplicates(users, 'email');
console.log(duplicateUsersByEmail);
Output
[
{ id: 3, name: 'Ami Kumar', email: 'amit@example.com' },
{ id: 5, name: 'Amit Kumar', email: 'amit@example.com' }
]
2. Using Array Methods
You can also use array methods like filter and some to find duplicates. This method is more declarative and uses JavaScript's higher-order functions.
Steps to Implement using Array Methods
- Use filter to Iterate Over the Array
- Use some to Check for Duplicates
const users = [
{ id: 1, name: 'Amit Kumar', email: 'amit@example.com' },
{ id: 2, name: 'Sumit Kumar', email: 'sumit@example.com' },
{ id: 3, name: 'Ami Kumar', email: 'amit@example.com' },
{ id: 4, name: 'Raj Kumar', email: 'raj@example.com' },
{ id: 5, name: 'Amit Kumar', email: 'amit@example.com' }
];
function getDuplicates(arr, key) {
return arr.filter((item, index, self) =>
index !== self.findIndex((t) => (
t[key] === item[key]
))
);
}
const duplicateUsersByEmail = getDuplicates(users, 'email');
console.log(duplicateUsersByEmail);
Output
[
{ id: 3, name: 'Ami Kumar', email: 'amit@example.com' },
{ id: 5, name: 'Amit Kumar', email: 'amit@example.com' }
]
3. Using JSON Stringification
Another approach is to stringify the objects and then use a Set to find duplicates. This method is useful when dealing with complex objects where multiple keys need to be considered.
Steps to Implement using JSON Stringification
- Stringify Each Object
- Use a Set to Track Uniques and Duplicates
const users = [
{ id: 1, name: 'Amit Kumar', email: 'amit@example.com' },
{ id: 2, name: 'Sumit Kumar', email: 'sumit@example.com' },
{ id: 3, name: 'Ami Kumar', email: 'amit@example.com' },
{ id: 4, name: 'Raj Kumar', email: 'raj@example.com' },
{ id: 5, name: 'Amit Kumar', email: 'amit@example.com' }
];
function getDuplicates(arr) {
const seen = new Set();
const duplicates = [];
arr.forEach(item => {
const identifier = `${item.name}|${item.email}`;
if (seen.has(identifier)) {
duplicates.push(item);
} else {
seen.add(identifier);
}
});
return duplicates;
}
const duplicateUsers = getDuplicates(users);
console.log(duplicateUsers);
Output
[
{ id: 3, name: 'Ami Kumar', email: 'amit@example.com' },
{ id: 5, name: 'Amit Kumar', email: 'amit@example.com' }
]