
- 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
Sort the array and group all the identical (duplicate) numbers into their separate subarray in JavaScript
We are required to write a JavaScript function that takes in an array of numbers as one and the only input. The input array will probably contain some duplicate entries.
Our function should sort the array and group all the identical (duplicate) numbers into their separate subarray.
For example −
If the input array is −
const arr = [5, 7, 5, 7, 8, 9, 1, 1];
Then the output should be −
const output = [ [1, 1], [5, 5], [7, 7], [8], [9] ];
Example
The code for this will be −
const arr = [5, 7, 5, 7, 8, 9, 1, 1]; const sortAndGroup = (arr = []) => { let result = []; let groupArray; arr.sort((a, b) => a - b); for (let i = 0; i < arr.length; i++) { if (arr[i − 1] !== arr[i]) { groupArray = []; result.push(groupArray); }; groupArray.push(arr[i]); }; return result; }; console.log(sortAndGroup(arr));
Output
And the output in the console will be −
[ [ 1, 1 ], [ 5, 5 ], [ 7, 7 ], [ 8 ], [ 9 ] ]
- Related Articles
- Finding all duplicate numbers in an array with multiple duplicates in JavaScript
- Sum all duplicate value in array - JavaScript
- How to sort array by first item in subarray - JavaScript?
- Return a subarray that contains all the element from the original array that are larger than all the elements on their right in JavaScript
- Sum all duplicate values in array in JavaScript
- Formatting a string to separate identical characters in JavaScript
- Distance between 2 duplicate numbers in an array JavaScript
- MongoDB aggregation group and remove duplicate array values?
- How to separate alphabets and numbers from an array using JavaScript
- Finding all peaks and their positions in an array in JavaScript
- Grouping identical entries into subarrays - JavaScript
- How to unpack array elements into separate variables using JavaScript?
- Implementing insertion sort to sort array of numbers in increasing order using JavaScript
- MySQL group by for separate id without using GROUP BY to remove duplicate column row?
- Special type of sort of array of numbers in JavaScript

Advertisements