Open In App

JavaScript Program to Return all Elements of Array Less than X

Last Updated : 03 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We will return all elements of an array that are less than a given value X in JavaScript. Given an array arr and number and the task is to write a function that returns an array whose items are smaller than n.

Example:

Input array = [ 1 ,  2,  3 , 4 , 5 , 6 , 7 , 8 , 9]     X = 6 

Output: [ 1 , 2 , 3 , 4 , 5]

Using Filter Method

This utilizes the built-in filter method to create a new array containing elements less than X based on a provided condition function.

Example: Implementation to return all elements of array less than X using for loop approach.

JavaScript
function elementsLessThanX(arr, X) {
    return arr.filter(element => element < X);
}

const arr = [1, 3, 5, 7, 9];
const X = 6;
console.log("Elements less than", X, ":", elementsLessThanX(arr, X));

Output
Elements less than 6 : [ 1, 3, 5 ]

Time complexity: O(n)

Space complexity: O(n)

Using for loop

It iterates through the array and selectively adds elements less than X into a new array, offering control over the filtering process with explicit looping.

Example: Implementation to return all elements of array less than X using for loop approach.

JavaScript
function elementsLessThanX(arr, X) {
  let answer = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < X) {
      answer.push(arr[i]);
    }
  }
  return answer;
}

let array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let X = 6;
console.log(elementsLessThanX(array, X)); 

Output
[ 1, 2, 3, 4, 5 ]

Time complexity: O(n)

Space complexity: O(n)

Using Array Reduce Method

It uses the reduce method to accumulate elements less than X into a new array, providing a concise way to apply filtering logic while reducing the array.

Example: Implementation to return all elements of array less than X using for loop approach.

JavaScript
function elementsLessThanX(arr, X) {
    return arr.reduce((acc, curr) => {
        if (curr < X) {
            acc.push(curr);
        }
        return acc;
    }, []);
}

const arr = [1, 3, 5, 7, 9];
const X = 6;
console.log(
    "Elements less than", X, ":", elementsLessThanX(arr, X));

Output
Elements less than 6 : [ 1, 3, 5 ]

Time complexity: O(n)

Space complexity: O(n)

Using Array Iteration Method

It utilizes forEach to iterate through the array and selectively adds elements less than X into a new array, offering simplicity and readability for the filtering operation.

Example: Implementation to return all elements of array less than X using Array Iteration approach.

JavaScript
function elementsLessThanX(arr, X) {
    const result = [];
    arr.forEach(element => {
        if (element < X) {
            result.push(element);
        }
    });
    return result;
}

const arr = [1, 3, 5, 7, 9];
const X = 6;
console.log(
    "Elements less than", X, ":", elementsLessThanX(arr, X));

Output
Elements less than 6 : [ 1, 3, 5 ]

Time complexity: O(n)

Space complexity: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads