Counting duplicates and aggregating array of objects in JavaScript


The Given problem is stating to count the duplicates of array elements and then aggregating objects in a new array.

Understanding the Problem

Problem statement is saying to identify the duplicate elements from an array and make a single array of those objects stating the counts. To solve this question we will use brute force technique with the help of for loops.

What is an Aggregating Array of Objects?

Aggregation in an array refers to combining down several objects in a new array as an output. We have to check if the aggregate of objects is compatible and equal to present elements of the array mentioned .

The problem statement can be seen with the output given below:

Input

const details = [ 
  { skill: 'Python', user: 'Preet' }, 
  { skill: 'Javascript', user: 'Shreya' }, 
  { skill: 'Javascript', user: 'Kajal' }, 
  { skill: 'Java', user: 'Preet' }, 
  { skill: 'Javascript', user: 'Preet' }, 
  { skill: 'HTML and Css', user: 'Happy' } 
];

Output

[
{ "skill": "Python",
  "user": [
   "Preet",
   "Happy"
  ]
},
{
  "skill": "Javascript",
  "user": [
   "Shreya",
   "Kajal",
   "Preet"
  ]
},
{
  "skill": "Java",
  "user": [
   "Preet"
  ]
},
{
  "skill": "HTML and Css",
  "user": [
   "Happy"
  ]
}
]

Algorithm

Step 1: Input given in the array of objects is a mix of duplicate data and the same type, in our example there is name and type objects have duplicate values. So to aggregate them in a different array with their count value. To solve this problem, declaration of function with three parameters given in that and name them as data, pCount (property count), pGroup (property to group).

Step 2: Declare a count variable to count the occurrences of the object values in the data.

Step 3: Now we define multiple for loops to find out the same properties with their values. In our example we have 2 objects namely name and type. So I initialized two for loops to segregate properties and put them in a different array and one loop to count the objects of the same type.

Step 4: The resultant new arrays will come after passing all the conditions and we can visualize the output containing all multiple arrays with different data values in it.

Example

// Declaration of data 
const data = [
  { name: 'Orange', type: 'fruit' },
  { name: 'Orange', type: 'fruit' },
  { name: 'Pineapple', type: 'fruit' },
  { name: 'Pineapple', type: 'fruit' },
  { name: 'Potato', type: 'vegetable' },
  { name: 'Tomato', type: 'vegetable' },
  { name: 'spinach', type: 'vegetable' },
  { name: 'spinach', type: 'vegetable' }
];
// declaration of function with three arguments
function countCollect(data, pCount, pGroup) {
  var count = {};
  for (let obj of data) {
	let pValue = obj[pCount];
	if (pValue in count) {
     count[pValue]++;
	} else {
  	count[pValue] = 1;
	}
  }
  let result = {};
  for (let obj of data) {
	let gValue = obj[pGroup];
	if (gValue in result) {
     result[gValue].push(obj);
	} else {
     result[gValue] = [obj];
	}
  }
  for (let pValue in count) {
	result[pValue] = {
  	count: count[pValue]
	};
  }
  return result;
}
let result = countCollect(data, 'name', 'type');
console.log(result);

Output

{
  fruit: [
   { name: 'Orange', type: 'fruit' },
   { name: 'Orange', type: 'fruit' },
   { name: 'Pineapple', type: 'fruit' },
   { name: 'Pineapple', type: 'fruit' }
  ],
  vegetable: [
   { name: 'Potato', type: 'vegetable' },
   { name: 'Tomato', type: 'vegetable' },
   { name: 'spinach', type: 'vegetable' },
   { name: 'spinach', type: 'vegetable' }
  ],
  Orange: { count: 2 },
  Pineapple: { count: 2 },
  Potato: { count: 1 },
  Tomato: { count: 1 },
  spinach: { count: 2 }
}

In the above output we can see there is a one array containing different types of data. Initial block of output shows for fruits, second block of output shows for vegetables with their name and type. The last block of output shows the counting of fruits and vegetables.

Complexity

The time taken to execute this algorithm is big O of n square time. Because the algorithm is using two for loops to get the result. The space complexity will be calculated as per the length of the array to store all the elements in the memory. So with the help of complexities we can analyze the efficiency of the problem.

Conclusion

As we have seen the simplicity of the given problem. To solve any problem we need to follow certain steps and think logically. Time complexity and space complexity are two measures of the efficiency of an algorithm. The above problem takes different approaches to calculate the time space complexity.

Updated on: 18-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements