Numbers smaller than the current number JavaScript

In JavaScript, we often need to count how many numbers in an array are smaller than each element. This problem requires comparing each element against all others to build a result array showing the count of smaller numbers for each position.

Understanding the Problem

Given an array of integers, we need to return a new array of the same length where each element represents the count of numbers smaller than the corresponding element in the original array.

For example: if we have [5, 4, 3, 2, 1], the output should be [4, 3, 2, 1, 0] because:

  • 5 has 4 smaller numbers (4, 3, 2, 1)
  • 4 has 3 smaller numbers (3, 2, 1)
  • 3 has 2 smaller numbers (2, 1)
  • 2 has 1 smaller number (1)
  • 1 has 0 smaller numbers

Using Nested Loops (Simple Approach)

function smallerThanCurrent(nums) {
    const result = [];
    
    for (let i = 0; i < nums.length; i++) {
        let count = 0;
        for (let j = 0; j < nums.length; j++) {
            if (nums[j] < nums[i]) {
                count++;
            }
        }
        result.push(count);
    }
    
    return result;
}

const arr = [8, 1, 2, 2, 3];
console.log(smallerThanCurrent(arr));
[4, 0, 1, 1, 3]

Using reduce() and filter() Methods

const smallerThanCurrentFunctional = (arr) => {
    return arr.reduce((result, currentNum) => {
        const count = arr.filter(num => num < currentNum).length;
        result.push(count);
        return result;
    }, []);
};

const numbers = [5, 4, 3, 2, 1];
console.log(smallerThanCurrentFunctional(numbers));
[4, 3, 2, 1, 0]

Using map() Method (Concise)

const smallerThanCurrentMap = (arr) => {
    return arr.map(num => arr.filter(x => x < num).length);
};

const testArray = [6, 5, 4, 8];
console.log(smallerThanCurrentMap(testArray));
[2, 1, 0, 3]

Comparison of Approaches

Method Time Complexity Readability Memory Usage
Nested Loops O(n²) High Low
reduce() + filter() O(n²) Medium Medium
map() + filter() O(n²) High Medium

Complete Example with Edge Cases

function handleEdgeCases(arr) {
    // Handle empty array
    if (!arr || arr.length === 0) return [];
    
    // Handle single element
    if (arr.length === 1) return [0];
    
    return arr.map(num => arr.filter(x => x < num).length);
}

console.log("Empty array:", handleEdgeCases([]));
console.log("Single element:", handleEdgeCases([5]));
console.log("Duplicates:", handleEdgeCases([1, 1, 2, 2, 2]));
console.log("Mixed numbers:", handleEdgeCases([7, 7, 7, 7]));
Empty array: []
Single element: [0]
Duplicates: [0, 0, 3, 3, 3]
Mixed numbers: [0, 0, 0, 0]

Time Complexity Analysis

All the above approaches have O(n²) time complexity because for each element, we need to compare it with all other elements in the array. For large datasets, consider using sorting-based approaches or frequency counting for better performance.

Conclusion

We've demonstrated multiple ways to count smaller numbers using nested loops, reduce() with filter(), and map() with filter(). The map() approach offers the most concise solution while maintaining good readability for most use cases.

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

632 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements