Merge duplicate and increase count for each object in array in JavaScript

The problem statement says to merge duplicate elements and increase the count for every object present in the array and implement this program in javascript.

Understanding the Problem

To start coding we need to understand the basic functionality of the javascript functions. Predefined functions in JavaScript will complete half of our task. Merging and eliminating duplicates are the fundamental array optimization operations that can be performed. In this issue, we will define a count variable that will count how many entries there are in the array.

We will go through each item in the array using the reduce() method to count and decrease the array's size by removing duplicates. As a result, the reduction method starts with an initial value and uses a callback function to accumulate values.

In our example, a variety of fruits with the two qualities fruit and count are used. To increase the count value in this, we will merge them if they have the same fruit value.

Input Array = [
   {fruit: "Banana", count: 12},
   {fruit: "Kiwi", count: 10},
   {fruit: "Banana", count: 15},
   {fruit: "Kiwi", count: 9},

o/p Array = [
   {fruit: "Banana", count: 27},
   {fruit: "Kiwi", count: 19},


Here's the algorithm for the program:

Step 1: Create an array mergeAndCount to store the merged objects. This step will initialize the first operation to check duplicates.

Step 2: Use a callback function and an empty starting value to call the reduce method on the fruits array ([]).

Step 3: Determine whether the an array contains an object with the same name as the c object in the callback function: Otherwise, include the c item in the an array. If so, add the count of the c object to the already existing count of the item in a.

Step 4: Return the "a" array from the reduce function in step 4.

Step 5: Return the mergeAndCount to the console.


// define a function to check power of 3
const fruits = [
   {fruit: "Apple", count: 12},
   {fruit: "Orange", count: 10},
   {fruit: "Pineapple", count: 5},
   {fruit: "Apple", count: 10},
   {fruit: "Orange", count: 4},
   {fruit: "Pineapple", count: 6},
   {fruit: "Banana", count: 12},
   {fruit: "Pineapple", count: 3}

const mergeAndCount = fruits.reduce((a, c) => {
   const obj = a.find((obj) => obj.fruit === c.fruit);
      obj.count += c.count;
   return a;
}, []);

console.log("After counting and merging:");


After counting and merging:
  { fruit: 'Apple', count: 22 },
  { fruit: 'Orange', count: 14 },
  { fruit: 'Pineapple', count: 14 },
  { fruit: 'Banana', count: 12 }


// define an array
const data = [
  { name: 'apple', category: 'fruit' },
  { name: 'orange', category: 'fruit' },
  { name: 'banana', category: 'fruit' },
  { name: 'pear', category: 'fruit' },
  { name: 'apple', category: 'fruit' },
  { name: 'broccoli', category: 'vegetable' },
  { name: 'carrot', category: 'vegetable' },
  { name: 'spinach', category: 'vegetable' },
  { name: 'spinach', category: 'vegetable' },
  { name: 'spinach', category: 'vegetable' }

// create a function to merge and count
function mergeDuplicates(data, propToMerge) {
  let counts = {};
  for (let obj of data) {
   let propValue = obj[propToMerge];
   if (propValue in counts) {
   } else {
     counts[propValue] = 1;
     counts[propValue + '_data'] = [obj];

  let result = [];
  for (let propValue in counts) {
   if (counts[propValue] > 1 && propValue !== propValue + 
'_data') {
     result.push({ [propToMerge]: propValue, count: counts[propValue], 
data: counts[propValue + '_data'] });

  return result;

// call the mergeDuplicates 

const result = mergeDuplicates(data, 'name');


  { name: 'apple', count: 2, data: [ [Object] ] },
  { name: 'spinach', count: 3, data: [ [Object] ] }

Time complexity

The time complexity for the above code is O(n). Because the time taken to complete the execution is equal to the length of the array. And space complexity is also O(n), to store n number of elements in the array.


This is the basic idea behind solving this kind of problem. In the overall process we have used a function called mergeAndCount(), arithmetic operators and comparison operators to solve the problem. And saw how to calculate the time and space complexity of the algorithm.

Updated on: 18-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started