
- 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
Check if some elements of array are equal JavaScript
We have an array of numbers that have got some redundant entries, our job is 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 = [1, 3, 3, 1]; //then the output should be: const output = [[1, 1], [3, 3]];
We will use a HashMap to keep a track of the elements already occurred and iterate over the array using a for loop, the code for this will be −
Example
const arr = [1, 3, 3, 1]; 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 −
[ [ 1, 1 ], [ 3, 3 ] ]
- Related Articles
- Python – Check if elements index are equal for list elements
- Check if array elements are consecutive in Python
- Python – Check if elements in a specific index are equal for list elements
- Check if all array elements are distinct in Python
- Are array of numbers equal - JavaScript
- Check if values of two arrays are the same/equal in JavaScript
- Check if elements of array can be made equal by multiplying given prime numbers in Python
- Python – Check if Splits are equal
- Check if three consecutive elements in an array is identical in JavaScript
- Check if the array has an element which is equal to product of remaining elements in Python
- Check if all elements of the array are palindrome or not in Python
- JavaScript Checking if all the elements are same in an array
- Check if the array has an element which is equal to sum of all the remaining elements in Python
- How to check if some specific columns of an R data frame are equal to a column or not?
- Check if product of Array elements in given range are M-th root or not

Advertisements