
- 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
Find array elements that are out of order in JavaScript
Suppose we have an array of sorted numbers but some elements of the array are out of their sorted order.
We are required to write a JavaScript function that takes in one such array and returns a subarray of all those elements that are out of order.
Example
The code for this will be −
const arr = ["2", "3", "7", "4", "5", "6", "1"]; const findOutOfOrder = arr => { let notInOrder = []; notInOrder = arr.filter((el, ind) => { return ind && this.next !== +el || (this.next = +el + 1, false); }, { next: null }); return notInOrder; }; console.log(findOutOfOrder(arr));
Output
The output in the console −
[ '7', '1' ]
- Related Articles
- JavaScript Program to Find k maximum elements of array in original order
- How do find out all elements that match a specific condition in JavaScript?
- Find k maximum elements of array in original order in C++
- Elements that appear twice in array in JavaScript
- Frequency of elements of one array that appear in another array using JavaScript
- Currified function that multiples array elements in JavaScript
- JavaScript array: Find all elements that appear more than n times
- C# Program to order array elements in descending order
- Find smallest subarray that contains all elements in same order in C++
- Subtracting array in JavaScript Delete all those elements from the first array that are also included in the second array
- Check if some elements of array are equal JavaScript
- MongoDB query to change order of array elements?
- Find document that matches same array elements in MongoDB?
- Program to find out the sum of evenly spaced elements in an array in Python
- C# Program to order array elements

Advertisements