JavaScript - Count Number of Occurrences of Repeated Names in an Array of Objects
Last Updated :
10 Jan, 2025
Here are the various methods to count the number of occurrences of repeated names in an array of objects in JavaScript
1. Using a Basic Loop with an Object
This is the simplest approach that uses a for loop and an object to store the counts.
JavaScript
//Driver Code Starts
const a = [
{ name: "Alia" }, { name: "Bharat" }, { name: "Shriya" }, { name: "Charu" },
{ name: "Tanu" }, { name: "Tanya" },
];
//Driver Code Ends
const Loop = (arr) => {
const c = {};
for (const { name } of arr) {
c[name] = (c[name] || 0) + 1;
}
return c;
};
//Driver Code Starts
const res = Loop(a);
console.log(res);
//Driver Code Ends
Output{ Alia: 1, Bharat: 1, Shriya: 1, Charu: 1, Tanu: 1, Tanya: 1 }
In this example:
- The function iterates over the array, extracting the name property from each object.
- It initializes or increments the count for each name in the counts object.
2. Using Map
The code counts repeated names in an array of objects by extracting names, identifying unique ones, and mapping them with their respective counts into a new array of objects.
JavaScript
//Driver Code Starts
const a = [{ name: "Riya" }, { name: "Nisha" }, { name: "Tanisha" }, { name: "Jaya" },
{ name: "Bobby" }, { name: "Alia" },
];
//Driver Code Ends
const map = (arr) => {
const nameMap = new Map();
arr.forEach(({ name }) => {
nameMap.set(name, (nameMap.get(name) || 0) + 1);
});
return Object.fromEntries(nameMap);
};
//Driver Code Starts
const res = map(a);
console.log(res);
//Driver Code Ends
Output{ Riya: 1, Nisha: 1, Tanisha: 1, Jaya: 1, Bobby: 1, Alia: 1 }
In this example:
- The function map counts occurrences of names in an array of objects.
- It uses a Map to store names as keys and their counts as values, updating the count for each name during iteration.
- Finally, it converts the Map to an object and returns the result.
3. Using Array.prototype.reduce
The reduce
method allows a more functional programming approach.
JavaScript
//Driver Code Starts
const a = [{ name: "Riya" }, { name: "Nisha" }, { name: "Tanisha" }, { name: "Jaya" },
{ name: "Bobby" }, { name: "Alia" },
];
//Driver Code Ends
const reduce = (arr) => {
return arr.reduce((acc, { name }) => {
acc[name] = (acc[name] || 0) + 1;
return acc;
}, {});
};
//Driver Code Starts
const res = reduce(a);
console.log(res);
//Driver Code Ends
Output{ Riya: 1, Nisha: 1, Tanisha: 1, Jaya: 1, Bobby: 1, Alia: 1 }
In this example:
- The reduce function iterates through the array, updating an accumulator object with the count of each name.
- This method is concise and expressive.
4. Using Array.prototype.filter
This approach uses filter to count occurrences for each unique name. While less efficient, it’s simpler for small datasets.
JavaScript
//Driver Code Starts
const a = [{ name: "Riya" }, { name: "Nisha" }, { name: "Tanisha" }, { name: "Jaya" },
{ name: "Bobby" }, { name: "Alia" },
];
//Driver Code Ends
const filter = (arr) => {
const b = [...new Set(arr.map(({ name }) => name))];
return b.reduce((acc, Name) => {
acc[b] = arr.filter(({ b }) => b === Name).length;
return acc;
}, {});
};
//Driver Code Starts
const res = filter(a);
console.log(res);
//Driver Code Ends
Output{ 'Riya,Nisha,Tanisha,Jaya,Bobby,Alia': 0 }
In this example:
- Extract unique names using Set.
- Use filter to count occurrences for each unique name.
- Combine results into an object.
5. Using External Libraries
For complex projects, using libraries like Lodash can simplify such operations.
JavaScript
const _ = require("lodash");
const a = [{ name: "Riya" }, { name: "Nisha" }, { name: "Tanisha" }, { name: "Jaya" },
{ name: "Bobby" }, { name: "Alia" },
];
const res = _.countBy(a, "name");
console.log(res);
In this example:
- The _.countBy method groups and counts occurrences of names directly.
- This approach is concise and reliable for larger codebases.
Choosing the Right Approach
Approach | Suitable For | Complexity |
---|
Basic Loop | Simplicity, small datasets | O(n) |
reduce Method | Functional programming | O(n) |
Map | Efficiency, large datasets | O(n) |
filter Method | Simplicity, small datasets | O(n^2) |
External Libraries | Large projects | O(n) |
Similar Reads
Count Occurrences of All Items in an Array in JavaScript Here are the different methods to count the occurrences of all items in an array in JavaScript1. Using forEach() MethodThe arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.
3 min read
JavaScript - How To Get Distinct Values From an Array of Objects? Here are the different ways to get distinct values from an array of objects in JavaScript1. Using map() and filter() MethodsThis approach is simple and effective for filtering distinct values from an array of objects. You can use map() to extract the property you want to check for uniqueness, and th
4 min read
How to Remove Duplicate Objects from an Array in JavaScript? In JavaScript, it's a common example that the arrays contain objects and there might be a possibility that the objects may or may not be unique. Removing these duplicate objects from the array and getting the unique ones is a common task in Web Development. These are the following approaches: Table
2 min read
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 ProblemLet's assume we have an arra
4 min read
Count Unique Elements in Array Without Sorting using JavaScript One can count unique elements(distinct elements) present in an array without sorting JavaScript. There are several methods of counting unique elements without sorting in JavaScript. Below is an example to understand the problem clearly. Example:Input: [ 1,2, 3, 1, 3, 4, 5, 5, 2] Output: 5 Explanatio
3 min read