

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers. Our function should construct an output array based on the input array.
For each corresponding element our output array should contain the count of number smaller than that number to its right. Finally, we should return this array.
Example
Following is the code −
const arr = [6, 2, 8, 5, 1, 3]; const buildSmallerArray = (arr = []) => { let count; let base; const res = []; for (let i = 0; i < arr.length; i++) { base = arr[i]; count = 0; for (let j = i + 1; j < arr.length; j++) { if (arr[j] < base) count++; }; res.push(count); }; return res; }; console.log(buildSmallerArray(arr));
Output
[ 4, 1, 3, 2, 0, 0 ]
- Related Questions & Answers
- Getting elements of an array depending on corresponding values of another JavaScript
- Sorting array based on increasing frequency of elements in JavaScript
- Constructing 2-D array based on some constraints in JavaScript
- Append the current array with the squares of corresponding elements of the array in JavaScript
- Grouping array of array on the basis of elements in JavaScript
- Constructing an array of first n multiples of an input number in JavaScript
- Constructing a sentence based on array of words and punctuations using JavaScript
- JavaScript - Constructs a new array whose elements are the difference between consecutive elements of the input array
- Rearranging elements of an array in JavaScript
- Modify an array based on another array JavaScript
- Constructing a string based on character matrix and number array in JavaScript
- 8086 program to determine modulus of first array elements corresponding to another array elements
- 8086 program to determine product of corresponding elements of two array elements
- Count smaller elements in sorted array in C++
- Equality of corresponding elements in JavaScript
Advertisements