Skip to content
Related Articles

Related Articles

How to write a function that returns array elements larger than a number in JavaScript ?

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Jul, 2022
View Discussion
Improve Article
Save Article

Given an array arr and number n and the task is to write a function that returns an array whose items are larger than n.

Example:

Input:   arr = [65, 16, 0, 6, 64, 1, 68]
         n = 16
Output:  [65, 64, 68]
  
Input:   arr = [6, 46, 54, 6, 56, 54, 65, 4, 65]
         n = 50
Output:  [54, 56, 54, 65, 65]

To achieve this we have the following approaches :

Approach 1: Using Array.filter()

In this approach, we use the Array.filter method.  At every iteration, we check if the value is greater than num or not.

Example:

Javascript




<script>
let returnLarger = (arr, num) => arr.filter(n => n > num);
 
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>


Output:

[65, 64, 68]
[54, 56, 54, 65, 65]

Approach 2: Using Array.reduce()

In this approach, we use the array.reduce method. Initialize the accumulator using an empty array and at every iteration check if the current value is greater than num then concatenate the current value with the accumulator and return it otherwise return the accumulator as it is.

Example:

Javascript




<script>
// Creating a function that return an array
// whose items are less than num
let returnLarger = (arr, num) => {
  return arr.reduce((acc, curr)=>{ 
    if(curr > num){
      return acc.concat(curr)  // Concatenate the acc with arr
    }else{
      return acc
    }
  }, [])   // Initialize the accumulator with an empty array
}
 
 
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>


Output:

[65, 64, 68]
[54, 56, 54, 65, 65]

Approach 3: Using the Array.map method:

In this approach, we iterate the array using the map method and at every iteration, we check if the current value is greater than num or not? If true, then we return the value as it. If false, then replace the item with an empty string (Or any falsy value). After that using the filter method remove the falsy values.

Javascript




<script>
// Creating a function that return an array
// whose items are less than num
let returnLarger = (arr, num) => {
  return arr.map(v => v > num ? v : "").filter(Boolean)
}
 
 
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>


Output:

[65, 64, 68]
[54, 56, 54, 65, 65]

Approach 4: using the for loop:

In this approach, we create an empty array and iterate the given array using the for loop and at every iteration, we check the current value is greater than num or not. If true, then we push the value in the newly created array.

Javascript




<script>
// Creating a function that return an array
// whose items are less than num
 
let returnLarger = (arr, num) => {
  let newArr = []
    
  for(let i = 0; i < arr.length; i++){
    if(arr[i] > num){
      newArr.push(arr[i])
    }
  }
   
  return newArr
}
 
 
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>


Output:

[65, 64, 68]
[54, 56, 54, 65, 65]

Approach 5: using the forEach loop:

In this approach, we create an empty array and iterate the given array using forEach loop and at every iteration, we perform expression with and operator where first operator checks the current value is greater than num or not if it return false new operation is not performed and if return true then it push the element to array.

Javascript




<script>
// Creating a function that return an array
// whose items are less than num
 
let returnLarger = (arr, num) => {
let newArr = []
     
arr.forEach( ele => (ele > num) && newArr.push(ele))
 
return newArr
}
 
 
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
 
</script>


Output:

[ 65, 64, 68 ]
[ 54, 56, 54, 65, 65 ]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!