Convert array with duplicate values to object with number of repeated occurrences in JavaScript

When working with arrays that contain duplicate values, you often need to count occurrences and convert them into a structured format. This article shows how to transform an array with duplicates into an array of objects containing each unique value and its count.

Problem Statement

Suppose we have an array with duplicate entries like this:

const arr = ['California','Texas','Texas','Texas','New York','Missouri','New Mexico','California'];
console.log("Original array:", arr);
Original array: [
  'California', 'Texas',
  'Texas',      'Texas',
  'New York',   'Missouri',
  'New Mexico', 'California'
]

We need to create a function that converts this array into an array of objects, where each object contains a unique entry and its occurrence count:

[
  { name: 'California', count: 2 },
  { name: 'Texas', count: 3 },
  { name: 'New York', count: 1 },
  { name: 'Missouri', count: 1 },
  { name: 'New Mexico', count: 1 }
]

Using forEach() with findIndex()

This approach iterates through the array and uses findIndex() to check if an item already exists in the result array:

const arr = ['California','Texas','Texas','Texas','New York','Missouri','New Mexico','California'];

const findOccurrences = (arr = []) => {
    const res = [];
    arr.forEach(el => {
        const index = res.findIndex(obj => {
            return obj['name'] === el;
        });
        if(index === -1){
            res.push({
                "name": el,
                "count": 1
            });
        } else {
            res[index]["count"]++;
        }
    });
    return res;
};

console.log(findOccurrences(arr));
[
  { name: 'California', count: 2 },
  { name: 'Texas', count: 3 },
  { name: 'New York', count: 1 },
  { name: 'Missouri', count: 1 },
  { name: 'New Mexico', count: 1 }
]

Using reduce() (More Efficient)

A more efficient approach uses reduce() with a Map for O(1) lookups:

const arr = ['California','Texas','Texas','Texas','New York','Missouri','New Mexico','California'];

const countOccurrences = (arr) => {
    const countMap = arr.reduce((acc, item) => {
        acc[item] = (acc[item] || 0) + 1;
        return acc;
    }, {});
    
    return Object.entries(countMap).map(([name, count]) => ({
        name,
        count
    }));
};

console.log(countOccurrences(arr));
[
  { name: 'California', count: 2 },
  { name: 'Texas', count: 3 },
  { name: 'New York', count: 1 },
  { name: 'Missouri', count: 1 },
  { name: 'New Mexico', count: 1 }
]

Performance Comparison

Method Time Complexity Readability Best For
forEach + findIndex O(n²) Good Small arrays
reduce + Object O(n) Excellent Large arrays

Conclusion

Both methods effectively count array occurrences and convert them to objects. Use reduce() for better performance with large datasets, or forEach() for simpler, more readable code with small arrays.

Updated on: 2026-03-15T23:19:00+05:30

782 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements