
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Number of smaller and larger elements - JavaScript
Suppose, we have an array of literals like this −
const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9];
We are required to write a JavaScript function that takes in this array and a number, say n, and returns an object representing the count of elements greater than and smaller than n.
Example
Following is the code −
const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; const greaterSmallerNumbers = (arr, num) => { return arr.reduce((acc, val) => { let { greater, smaller } = acc; if(val > num){ greater++; }; if(val < num){ smaller++; }; return { greater, smaller }; }, { greater: 0, smaller: 0 }); }; console.log(greaterSmallerNumbers(arr, 3));
Output
Following is the output in the console −
{ greater: 8, smaller: 1 }
- Related Articles
- Frequency of smaller and larger elements - JavaScript
- Maximum difference between two elements such that larger element appears after the smaller number in C
- Find Number of Array Elements Smaller than a Given Number in Java
- Number of Larger Elements on right side in a string in C++
- One number is three times another. If the larger number is subtracted from $48$ the result is $4$ less than the smaller number subtracted from $32$ find the larger number.
- Numbers smaller than the current number JavaScript
- Two numbers are in the ratio 9: 2. If the smaller number is 320, find the larger number.
- Finding the element larger than all elements on right - JavaScript
- Unpack elements and set the unpack count larger than the available number of bits in Numpy
- Just smaller number with monotone digits in JavaScript
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- The difference of squares of two numbers is 180. The square of the smaller number is 8 times the larger number. Find the two numbers.
- Number of elements smaller than root using preorder traversal of a BST in C++
- Program to return number of smaller elements at right of the given list in Python
- Counting smaller and greater in JavaScript

Advertisements