
- 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
Combining the identical entries together in JavaScript
We have an array of numbers that have got identical entries. We are required to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed.
For example: If the input array is −
const arr = [234, 65, 65, 2, 2, 234];
// then the output should be −
const output = [[234, 234], [65, 65], [2, 2]];
We will use a Hashmap to keep a track of the elements already occurred and iterate over the array using a for loop.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr = [234, 65, 65, 2, 2, 234]; const groupArray = arr => { const map = {}; const group = []; for(let i = 0; i < arr.length; i++){ if(typeof map[arr[i]] === 'number'){ group[map[arr[i]]].push(arr[i]); }else{ //the push method returns the new length of array //and the index of newly pushed element is length-1 map[arr[i]] = group.push([arr[i]])-1; } }; return group; } console.log(groupArray(arr));
Output
The output in the console will be −
[ [ 234, 234 ], [ 65, 65 ], [ 2, 2 ] ]
- Related Articles
- Grouping identical entries into subarrays - JavaScript
- Removing identical entries from an array keeping its length same - JavaScript
- Combining two arrays in JavaScript
- Alternatingly combining array elements in JavaScript
- Adding up identical elements in JavaScript
- Sum identical elements within one array in JavaScript
- Split array entries to form object in JavaScript
- Combining multiple images into a single one using JavaScript
- Formatting a string to separate identical characters in JavaScript
- Merging sorted arrays together JavaScript
- Adding binary strings together JavaScript
- Retrieving object's entries in order with JavaScript?
- Combining two heatmaps in seaborn
- Finding intersection of arrays that contain repetitive entries in JavaScript
- Can all array elements mesh together in JavaScript?

Advertisements