
- 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
Removing letters to make adjacent pairs different using JavaScript
Problem
We are required to write a JavaScript function that takes in a string that contains only ‘A’, ‘B’ and ‘C’. Our function should find the minimum number of characters needed to be removed from the string so that the characters in each pair of adjacent characters are different.
Example
Following is the code −
const str = "ABBABCCABAA"; const removeLetters = (str = '') => { const arr = str.split('') let count = 0 for (let i = 0; i < arr.length; i++) { if (arr[i] === arr[i + 1]) { count += 1 arr.splice(i, 1) i -= 1 } } return count } console.log(removeLetters(str));
Output
3
- Related Articles
- Program to find shortest string after removing different adjacent bits in Python
- Removing adjacent duplicates from a string in JavaScript
- Counting adjacent pairs of words in JavaScript
- Removing smallest subarray to make array sum divisible in JavaScript
- Counting rings in letters using JavaScript
- Removing a Number from Array to make It Geometric Progression using C++
- Removing punctuations from a string using JavaScript
- Program to restore the array from adjacent pairs in Python
- Removing all spaces from a string using JavaScript
- Program to make pairwise adjacent sums small in Python
- Draw a rough sketch of a quadrilateral KLMN. State,(a) two pairs of opposite sides,(b) two pairs of opposite angles,(c) two pairs of adjacent sides,(d) two pairs of adjacent angles.
- Removing Elements from a Double Linked List using Javascript
- In the figure, write all pairs of adjacent angles and all the linear pairs."\n
- Generating desired pairs within a range using JavaScript
- How to make a text italic using JavaScript

Advertisements