Extract unique objects by attribute from array of objects
Last Updated :
11 Jul, 2025
Given an array of objects and the task is to return the unique object by the attribute.
Examples:
Input:
[
{ name: 'Geeks', id: 10 },
{ name: 'GeeksForGeeks', id: 10 },
{ name: 'Geeks', id: 20 },
{ name: 'Geeks', id: 10 }
]
Output:
[
{ name: 'Geeks', id: 10 },
{ name: 'GeeksForGeeks', id: 10 }
]
Approach: Let's assume that name is an attribute that differentiates the objects and needs the object with a minimum id number if multiple objects exist for the same name. Use the map to store objects and check whether similar objects were seen or not.
- Initialize an empty map.
- Iterate through the array using the filter() method.
- Check if there is any entry in the map with the same name as of current object.
- If true: i.e. there exists an entry with the same name then, check if its id is less than the current object's id.
- If true: i.e current object's id is less than the id of the object returned by the map then delete the map entry and enter the current object and return true.
- if false: i.e. id of the current object is greater than the id of the object returned by the map then return false.
- If false: i.e. there is no entry in a map with the same name then enter the current object into the map.
- Print unique objects.
Example: In this example, we will extract unique objects by attribute from an array of objects.
C++
#include <iostream>
#include <vector>
#include <unordered_map>
struct Object {
std::string name;
int id;
};
int main() {
std::vector<Object> objects = {
{"Geeks", 10},
{"GeeksForGeeks", 10},
{"Geeks", 20},
{"Geeks", 10}
};
std::unordered_map<std::string, int> mymap;
std::vector<Object> unique;
for (const auto& el : objects) {
if (mymap.find(el.name) != mymap.end()) {
if (el.id < mymap[el.name]) {
mymap[el.name] = el.id;
unique.push_back(el);
}
} else {
mymap[el.name] = el.id;
unique.push_back(el);
}
}
for (const auto& obj : unique) {
std::cout << "Name: " << obj.name << ", ID: " << obj.id << std::endl;
}
return 0;
}
Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UniqueObjectsFilter {
public static void main(String[] args) {
// Given objects array
List<Map<String, Object>> objects = new ArrayList<>();
objects.add(Map.of("name", "Geeks", "id", 10));
objects.add(Map.of("name", "GeeksForGeeks", "id", 10));
objects.add(Map.of("name", "Geeks", "id", 20));
objects.add(Map.of("name", "Geeks", "id", 10));
// Initialize a map to store the minimum 'id' for each 'name'
Map<String, Integer> myMap = new HashMap<>();
// Initialize a list to store unique objects
List<Map<String, Object>> unique = new ArrayList<>();
// Loop through each object in the 'objects' list
for (Map<String, Object> el : objects) {
// Check if 'name' is already in the map
if (myMap.containsKey(el.get("name"))) {
// If yes, compare the current 'id' with the stored minimum 'id'
if ((int) el.get("id") < myMap.get(el.get("name"))) {
// If the current 'id' is smaller, update the minimum 'id' in the map
myMap.put((String) el.get("name"), (int) el.get("id"));
// Add the current object to the list of unique objects
unique.add(el);
}
} else {
// If 'name' is not in the map, add it with the current 'id'
myMap.put((String) el.get("name"), (int) el.get("id"));
// Add the current object to the list of unique objects
unique.add(el);
}
}
// Print the list of unique objects
System.out.println(unique);
}
}
Python3
# Given JavaScript code to filter unique objects based on 'name' with the minimum 'id'
objects = [
{"name": "Geeks", "id": 10},
{"name": "GeeksForGeeks", "id": 10},
{"name": "Geeks", "id": 20},
{"name": "Geeks", "id": 10}
]
# Initialize an empty dictionary to store the minimum 'id' for each 'name'
mymap = {}
# Initialize an empty list to store unique objects
unique = []
# Loop through each object in the 'objects' array
for el in objects:
# Check if 'name' is already in the dictionary
if el["name"] in mymap:
# If yes, compare the current 'id' with the stored minimum 'id'
if el["id"] < mymap[el["name"]]:
# If the current 'id' is smaller, update the minimum 'id' in the dictionary
mymap[el["name"]] = el["id"]
# Add the current object to the list of unique objects
unique.append(el)
else:
# If 'name' is not in the dictionary, add it with the current 'id'
mymap[el["name"]] = el["id"]
# Add the current object to the list of unique objects
unique.append(el)
# Print the list of unique objects
print(unique)
javascript
let objects = [{
name: 'Geeks',
id: 10
}, {
name: 'GeeksForGeeks',
id: 10
}, {
name: 'Geeks',
id: 20
}, {
name: 'Geeks',
id: 10
}];
let mymap = new Map();
let unique = objects.filter(el => {
const val = mymap.get(el.name);
if (val) {
if (el.id < val) {
mymap.delete(el.name);
mymap.set(el.name, el.id);
return true;
} else {
return false;
}
}
mymap.set(el.name, el.id);
return true;
});
console.log(unique);
Output[ { name: 'Geeks', id: 10 }, { name: 'GeeksForGeeks', id: 10 } ]
Similar Reads
How to extract value of a property as array from an array of objects ? We will try to understand that how we may extract the value of a property as an array from an array of objects in JavaScript with the help of certain examples.Pre-requisite: Array of Objects in JavaScriptExample:Input:[ { apple: 2, mango: 4, }, { apple: 3, mango: 6, }, { apple: 7, mango: 11, },]Outp
3 min read
How to Access Array of Objects in JavaScript ? Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How
4 min read
How to call the map method only if the element is an array? In this article, we are going to learn How to call the map method only if the element is an array, Given an object literal, let us say 'person'. It has a number of properties of various types. The task here is to map the values of the property only if the property is an Array itself. Instead of an o
3 min read
How to Get only the Objecte of Document in MongoDB In MongoDB, the ability to extract specific objects from documents is an important skill that can significantly enhance our data retrieval capabilities. MongoDB's flexible document model allows for nested structures, where documents can contain arrays or sub-documents. However, retrieving only the d
3 min read
How to Filter Object Array based on Attributes? One can use the filter() and reduce() functions in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array. The filter() funct
3 min read
How to get an array of function property names from own enumerable properties of an object in JavaScript ? Enumerable Properties: All the properties of an object that can be iterated using for..in loop or Object.keys() method are known as enumerable properties. In this article, we will see how to get an array of functions of an object that are enumerable. It can be achieved by following these 2 steps. Us
2 min read