
- 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
Counting smaller numbers after corresponding numbers in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of Numbers as the first and the only argument.
Our function should prepare a new array based on the input array. And each corresponding element of this new array should be the count of elements smaller than the corresponding element in the original array.
For example, if the input to the function is −
const arr = [4, 7, 1, 4, 7, 5, 3, 8, 9];
Then the output should be −
const output = [2, 4, 0, 1, 2, 1, 0, 0, 0];
Output Explanation:
Because number smaller than 4 to its right are 2 (1 and 3), for 7 its 4 (1, 4, 5, 3) and so on.
Example
The code for this will be −
const arr = [4, 7, 1, 4, 7, 5, 3, 8, 9]; const countSmaller = (array = [], num) => array.reduce((acc, val) => { if(val < num){ acc++; }; return acc; }, 0); const smallerArray = (arr = []) => { const res = []; for(let i = 0; i < arr.length; i++){ const el = arr[i]; res[i] = countSmaller(arr.slice(i, arr.length), el); }; return res; }; console.log(smallerArray(arr));
Output
The output in the console will be −
[ 2, 4, 0, 1, 2, 1, 0, 0, 0 ]
- Related Articles
- Counting numbers after decimal point in JavaScript
- Counting How Many Numbers Are Smaller Than the Current Number in JavaScript
- Count of Smaller Numbers After Self in C++
- Counting smaller and greater in JavaScript
- Numbers smaller than the current number JavaScript
- Counting the clusters of positive numbers - JavaScript Arrays
- Why are natural numbers called counting numbers?
- What is Counting numbers ?
- Counting n digit Numbers with all unique digits in JavaScript
- Counting specific digits used in squares of numbers using JavaScript
- Counting prime numbers from 2 upto the number n JavaScript
- Converting numbers into corresponding alphabets and characters using JavaScript
- Counting largest numbers in row and column in 2-D array in JavaScript
- Convert an array of binary numbers to corresponding integer in JavaScript
- Counting prime numbers that reduce to 1 within a range using JavaScript

Advertisements