Limiting elements occurrences to n times in JavaScript

Problem

We are required to write a JavaScript function that takes in an array of integers, arr, that may contain duplicates as the first argument, and a number, num, as the second and final argument.

The task of our function is to iterate through the array and check whether there exists some number that appears for more than n times in the array.

If there exists any such element, we should delete its extra occurrences to limit its occurrence to at most num.

For example, if the input to the function is:

Input

const arr = [4, 1, 3, 1, 4, 1, 3, 4, 2];
const num = 2;

Output

[4, 1, 3, 1, 4, 3, 2]

Output Explanation

Both 4 and 1 appeared thrice, so their third appearance is deleted

Using Object to Track Occurrences

The solution uses an object to count occurrences of each element and only includes elements that haven't exceeded the limit:

const arr = [4, 1, 3, 1, 4, 1, 3, 4, 2];
const num = 2;

const deleteExtra = (arr = [], num = 1) => {
    if(num === 0){
        return [];
    }
    
    const res = [];
    const map = {};
    
    for(let i = 0; i < arr.length; i++){
        const el = arr[i];
        map[el] = (map[el] || 0) + 1;
        
        if(map[el] <= num){
            res.push(el);
        }
    }
    
    return res;
};

console.log(deleteExtra(arr, num));
[ 4, 1, 3, 1, 4, 3, 2 ]

How It Works

The algorithm maintains a frequency map and processes elements in order:

  1. Initialize an empty result array and frequency object
  2. For each element, increment its count in the frequency map
  3. If the count is within the limit, add the element to the result
  4. Skip elements that would exceed the occurrence limit

Alternative Approach Using Map

Using JavaScript's Map object provides cleaner syntax:

const limitOccurrences = (arr, limit) => {
    const countMap = new Map();
    const result = [];
    
    for (const element of arr) {
        const count = countMap.get(element) || 0;
        
        if (count < limit) {
            result.push(element);
            countMap.set(element, count + 1);
        }
    }
    
    return result;
};

const testArray = [1, 2, 3, 2, 1, 2, 3, 3, 3, 1];
console.log(limitOccurrences(testArray, 2));
[ 1, 2, 3, 2, 1, 3 ]

Edge Cases

The function handles several edge cases:

// Empty array
console.log(deleteExtra([], 3));

// Limit of 0
console.log(deleteExtra([1, 2, 3], 0));

// No duplicates
console.log(deleteExtra([1, 2, 3, 4], 2));

// All same elements
console.log(deleteExtra([5, 5, 5, 5, 5], 2));
[]
[]
[ 1, 2, 3, 4 ]
[ 5, 5 ]

Time and Space Complexity

Complexity Value Explanation
Time O(n) Single pass through array
Space O(k) k is number of unique elements

Conclusion

This solution efficiently limits element occurrences by tracking counts with an object or Map. It maintains the original order while removing excess duplicates in linear time.

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

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements