
- 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
How to remove some items from array when there is repetition in JavaScript
We are required to write a JavaScript function that takes in an array of literals. Our function should return a new array with all the triplets filtered.
The code for this will be −
const arr1 = [1,1,1,3,3,5]; const arr2 = [1,1,1,1,3,3,5]; const arr3 = [1,1,1,3,3,3]; const arr4 = [1,1,1,1,3,3,3,5,5,5,5,5,5,5,5,5,5,5,5,7,7]; const removeTriplets = arr => { const hashMap = arr => arr.reduce((acc, val) => { if(val in acc){ acc[val]++; }else{ acc[val] = 1; }; return acc; }, {}); let res = []; let obj = hashMap(arr); for(let key in obj){ for(let i = 0; i < obj[key] % 3; i++){ res.push(key) }; } return res; } console.log(removeTriplets(arr1)); console.log(removeTriplets(arr2)); console.log(removeTriplets(arr3)); console.log(removeTriplets(arr4));
The output in the console −
[ '3', '3', '5' ] [ '1', '3', '3', '5' ] [] [ '1', '7', '7' ]
- Related Articles
- Remove duplicate items from an array with a custom function in JavaScript
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- How to remove items from a list in C#?
- How to remove duplicate elements from an array in JavaScript?
- How to remove falsy values from an array in JavaScript?
- JavaScript Remove random item from array and then remove it from array until array is empty
- How to remove certain number elements from an array in JavaScript
- How to remove every Nth element from an array JavaScript?
- How to remove an item from JavaScript array by value?
- Remove element from array referencing spreaded array in JavaScript
- Completely removing duplicate items from an array in JavaScript
- Python Program to remove items from set
- Java program to remove items from Set
- Remove values in an array by comparing the items 0th index in JavaScript?
- Golang Program To Remove The First Given Number Of Items From The Array

Advertisements