
- 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
Return a subarray that contains all the element from the original array that are larger than all the elements on their right in JavaScript
We are required to write a JavaScript function that takes in an array of numbers and returns a subarray that contains all the element from the original array that are larger than all the elements on their right.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [12, 45, 6, 4, 23, 23, 21, 1]; const largerThanRight = (arr = []) => { const creds = arr.reduceRight((acc, val) => { let { largest, res } = acc; if(val > largest){ res.push(val); largest = val; }; return { largest, res }; }, { largest: -Infinity, res: [] }); return creds.res; }; console.log(largerThanRight(arr));
Output
The output in the console will be −
[ 1, 21, 23, 45 ]
- Related Articles
- Finding the element larger than all elements on right - JavaScript
- JavaScript Return an array that contains all the strings appearing in all the subarrays
- Find smallest subarray that contains all elements in same order in C++
- JavaScript array: Find all elements that appear more than n times
- Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
- Return the sum of two consecutive elements from the original array in JavaScript
- Delete all the nodes from the list that are greater than x in C++
- Sort the array and group all the identical (duplicate) numbers into their separate subarray in JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- Find an array element such that all elements are divisible by it using c++
- From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript
- Count array elements that divide the sum of all other elements in C++
- Remove all the elements from an ArrayList that are in another Collection in Java
- Find the element before which all the elements are smaller than it, and after which all are greater in Python
- Return a splitted array of the string based on all the specified separators - JavaScript

Advertisements