

- 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
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 Questions & Answers
- How to find out all elements that matches a specific condition in JavaScript?
- Find k maximum elements of array in original order in C++
- JavaScript array: Find all elements that appear more than n times
- Frequency of elements of one array that appear in another array using JavaScript
- Elements that appear twice in array in JavaScript
- Currified function that multiples array elements in JavaScript
- Find smallest subarray that contains all elements in same order in C++
- Find document that matches same array elements in MongoDB?
- Find integers that divides maximum number of elements of the array in C++
- C# Program to order array elements in descending order
- Check if some elements of array are equal JavaScript
- Elements of an array that are not divisible by any element of another array in C++
- MongoDB query to change order of array elements?
- Find a number that divides maximum array elements in C++
- Find an array element such that all elements are divisible by it using c++
Advertisements