Percentile-Based Number Filter

Last Updated : 2 Feb, 2026

Calculates the percentile of a value by counting how many numbers in an array are less than or equal to it.

  • Applies the percentile formula to compute the percentage.
  • Useful for understanding a value’s position in a dataset.

Problem Statement

You are given an array of integers along with a single integer value. Your task is to determine how many elements in the array are less than or equal to the given value and then compute the percentile using the standard percentile formula in JavaScript.

Given the array [1, 2, 3, 4, 5, 6] and the integer value 6:

  • All six elements in the array are less than or equal to 6.
  • Since every element satisfies the condition, the percentile result is 100%.

There are several approaches to solving this particular problem. We have covered the following two approaches:

Approach 1: Using for-in Loop

This approach iterates through each array element using a for-in loop to calculate the percentile value.

  • Checks each element against the given value inside the loop.
  • Uses a ternary operator instead of if-else for concise logic.
  • Prints the final percentile result after completing the loop.

We will be using the JavaScript for-in loop to check how many numbers are less than or equal to a given number.

JavaScript
//pc = percentileCalculation
const pc = (arr, val) => {
    let result = 0;

    for (let i in arr) {
        result = result + (arr[i] < val ? 1 : 0) +
                 (arr[i] === val ? 0.5 : 0);
    }

    let resultDisplay = (result / arr.length) * 100;
    console.log(`${resultDisplay}%`);
};

// Function call
pc([1, 2, 3, 4, 5, 6], 5);

Output:

75%
  • Create a function pc(arr, val) and initialize result for percentile calculation.
  • Loop through the array using for...in, adding 1 for values less than val and 0.5 for equal values.
  • Convert result to a percentage by dividing by array length, multiplying by 100, and logging it with %.

Approach 2: Using reduce() Method

This approach uses the reduce() method to traverse the array and compute the percentile in a single pass.

  • Accumulates the result using an accumulator (acc) while iterating through values (v).
  • Compares each element with the given value inside reduce().
  • Returns the final percentile value after processing the entire array.

We will be using the Javascript reduce() method to check how many numbers are less than or equal to a given number.

JavaScript
//pc = percentileCalculation
const pc = (arr, val) =>
    (100 *
    arr.reduce(
        (acc, v) => acc + (v < val ? 1 : 0) + 
        (v === val ? 0.5 : 0),
        0
    )) /
    arr.length;
    
// Function call
console.log(pc([1, 2, 3, 4, 5, 6], 5));

Output:

75
  • Define an arrow function pc(arr, val) that uses reduce() to accumulate the percentile count.
  • Add 1 for values less than val and 0.5 for values equal to val, starting with an accumulator of 0.
  • Convert the accumulated value into a percentage by multiplying by 100, dividing by the array length, and logging the result.
Comment