
- 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
Using recursion to remove consecutive duplicate entries from an array - JavaScript
We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space.
For example, if the input array is −
const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1];
Then the output should be −
const output = [17, 12, 354, 1];
Example
Following is the code −
const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; const comp = (arr, len = 0, deletable = false) => { if(len < arr.length){ if(deletable){ arr.splice(len, 1); len--; } return comp(arr, len+1, arr[len] === arr[len+1]) }; return; }; comp(arr); console.log(arr);
Output
This will produce the following output in console −
[ 17, 12, 354, 1 ]
- Related Articles
- Using recursion to remove consecutive duplicate entries from an array in JavaScript
- How do I recursively remove consecutive duplicate elements from an array?
- How to remove duplicate elements from an array in JavaScript?
- Remove/ filter duplicate records from array - JavaScript?
- Swift Program to Remove Duplicate Elements From an Array
- Golang Program To Remove Duplicate Elements From An Array
- Remove duplicate items from an array with a custom function in JavaScript
- How to redundantly remove duplicate elements within an array – JavaScript?
- Removing consecutive duplicates from strings in an array using JavaScript
- Remove the duplicate value from array with images data in JavaScript
- How to remove duplicate entries by two keys in MongoDB?
- Program to remove duplicate entries in a list in Python
- How to remove duplicate property values in array – JavaScript?
- Finding product of an array using recursion in JavaScript
- Remove elements from array using JavaScript filter - JavaScript

Advertisements