Finding the group with largest elements with same digit sum in JavaScript

We are required to write a JavaScript function that takes in a positive integer, say n, as the only argument.

The function should first group the integers from 1 to n into subarrays where each subarray contains all elements with the same digit sum. Then the function should examine each subarray and return the length of the largest group (the group with the most elements).

Understanding Digit Sum

The digit sum is calculated by adding all digits of a number. For example:

  • Digit sum of 15 = 1 + 5 = 6
  • Digit sum of 23 = 2 + 3 = 5
  • Digit sum of 7 = 7

Example

If the input number is 15:

const num = 15;

The groups formed by digit sum are:

[1, 10], [2, 11], [3, 12], [4, 13], [5, 14], [6, 15], [7], [8], [9]

The largest groups are [1, 10], [2, 11], [3, 12], [4, 13], [5, 14], and [6, 15], each containing 2 elements. So the output should be 2.

Solution

const num = 67;

const countLargestGroup = (num = 1) => {
    if(num < 10){
        return num;
    }
    
    let res = 0;
    let temp = 0;
    let map = {};
    
    // Calculate digit sum for each number from 1 to num
    for(let i = 1; i <= num; i++){
        let sum = 0;
        let currentNum = i;
        
        // Calculate digit sum
        while (currentNum) {
            sum += currentNum % 10;
            currentNum = Math.floor(currentNum / 10);
        }
        
        // Count numbers with same digit sum
        if(map[sum] != undefined){
            map[sum]++;
        } else {
            map[sum] = 1;
        }
    }
    
    // Find the maximum group size
    for (const key of Object.keys(map)) {
        if(temp == map[key]){
            res++;
        }
        else if(temp < map[key]){
            res = 1;
            temp = map[key];
        }
    }
    
    return res;
};

console.log(countLargestGroup(num));

Output

4

How It Works

The algorithm works in three steps:

  1. Calculate digit sums: For each number from 1 to n, calculate its digit sum
  2. Group by digit sum: Use a map to count how many numbers have each digit sum
  3. Find largest group: Iterate through the map to find the maximum count and return how many groups have that maximum size

Step-by-Step Example with num = 15

const demonstrateGrouping = (num) => {
    let map = {};
    
    for(let i = 1; i <= num; i++){
        let sum = 0;
        let currentNum = i;
        
        while (currentNum) {
            sum += currentNum % 10;
            currentNum = Math.floor(currentNum / 10);
        }
        
        if(!map[sum]) map[sum] = [];
        map[sum].push(i);
    }
    
    console.log("Groups by digit sum:");
    for(const [digitSum, numbers] of Object.entries(map)) {
        console.log(`Digit sum ${digitSum}: [${numbers.join(', ')}] - Length: ${numbers.length}`);
    }
    
    // Find max length
    const maxLength = Math.max(...Object.values(map).map(arr => arr.length));
    console.log(`\nLargest group size: ${maxLength}`);
};

demonstrateGrouping(15);
Groups by digit sum:
Digit sum 1: [1, 10] - Length: 2
Digit sum 2: [2, 11] - Length: 2
Digit sum 3: [3, 12] - Length: 2
Digit sum 4: [4, 13] - Length: 2
Digit sum 5: [5, 14] - Length: 2
Digit sum 6: [6, 15] - Length: 2
Digit sum 7: [7] - Length: 1
Digit sum 8: [8] - Length: 1
Digit sum 9: [9] - Length: 1

Largest group size: 2

Conclusion

This solution efficiently groups numbers by their digit sum using a hash map and finds the size of the largest group. The time complexity is O(n × d) where d is the average number of digits, and space complexity is O(k) where k is the number of unique digit sums.

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

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements