# Problem

`AAAA → A = 3 deletedBBBBB → B = 4 deletedABABABAB → ABABABAB = 0 deletedBABABA → BABABA = 0 deletedAAABBB → AB = 4 deleted`

Write a function or functions that returns the number sequential duplicates deleted from (s)

# Covering Our Bases

`function alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          // continue     }          return result; }`

# Understanding The Problem

## Convert To Array

`function alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          let arr = s.split('');     }          return result; }`

## Get Past Reference

`function alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          let arr = s.split('');                    let past = arr;      }          return result; }`

## Traverse Array

`function alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          let arr = s.split('');                    let past = arr;                    arr = arr.filter((item, key) => {               if (item !== past && key !== 0) {                    return item;                }            });        }          return result; }`

## Combine And Compare

`// Example// AAABBBfunction alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          let arr = s.split(''); // ['A','A','A','B','B','B']                    let past = arr; // 'A'                    arr = arr.filter((item, key) => {               if (item !== past && key !== 0) {                    // 'A' != 'A', 0 !== 0 = false                    // 'A' != 'A', 1 !== 0 = false                    // 'A' != 'A', 2 !== 0 = false                    // 'B' != 'A', 3 !== 0 = true                    // 'B' != 'A', 4 !== 0 = true (Something wrong!)                    return item;                }            });           // ['B', 'B', 'B']       }          return result; }`
`// Example// AAABBBfunction alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          let arr = s.split(''); // ['A','A','A','B','B','B']                    let past = arr; // 'A'                    arr = arr.filter((item, key) => {               if (item !== past && key !== 0) {                    // redefine past as the current item                    past = item;                    // 'A' != 'A', 0 !== 0 = false                    // 'A' != 'A', 1 !== 0 = false                    // 'A' != 'A', 2 !== 0 = false                    // 'B' != 'A', 3 !== 0 = true                    // 'B' != 'B', 4 !== 0 = false                    // 'B' != 'B', 5!== 0 = false                     return item;                }            });          // ['B']        }          return result; }`
`// Example// AAABBBfunction alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          let arr = s.split(''); // ['A','A','A','B','B','B']                    let past = arr; // 'A'                    arr = arr.filter((item, key) => {               if (item !== past && key !== 0) {                    // redefine past as the current item                    past = item;                    // 'A' != 'A', 0 !== 0 = false                    // 'A' != 'A', 1 !== 0 = false                    // 'A' != 'A', 2 !== 0 = false                    // 'B' != 'A', 3 !== 0 = true                    // 'B' != 'B', 4 !== 0 = false                    // 'B' != 'B', 5!== 0 = false                     return item;                }            });          // ['B']          result = [ s.charAt(0), ...arr ];          // ['A', 'B']       }          return result; }`

## Getting The Deletes

`// Example// AAABBBfunction alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          let arr = s.split(''); // ['A','A','A','B','B','B']                    let past = arr; // 'A'                    arr = arr.filter((item, key) => {               if (item !== past && key !== 0) {                    // redefine past as the current item                    past = item;                    // 'A' != 'A', 0 !== 0 = false                    // 'A' != 'A', 1 !== 0 = false                    // 'A' != 'A', 2 !== 0 = false                    // 'B' != 'A', 3 !== 0 = true                    // 'B' != 'B', 4 !== 0 = false                    // 'B' != 'B', 5!== 0 = false                     return item;                }            });          // ['B']          result = s.length - [ s.charAt(0), ...arr ].length;          // 'AAABBB'.length - ['A', 'B'].length     }          return result;     // 2}`

# Solution

`function alternatingCharacters(s) {     let result = 0;      if (s.length >= 1 && s.length <= 100000) {          let arr = s.split('');          let past = arr;                    arr = arr.filter((item, key) => {               if (item !== past && key !== 0) {                    past = item;                    return item;                }            });          result = s.length - [ s.charAt(0), ...arr ].length;     }          return result; }`

# Test Cases

`// s = "", Expected 0// s = "A", Expected 0// s = "B", Expected 0// s = "AB", Expected 0// s = "ABB", Expected 1// s = "BBBBB", Expected 4// s = "ABABABAB", Expected 0// s = "BABABA", Expected 0// s = "AAABBB", Expected 4alternatingCharacters(""); // 0 ✅alternatingCharacters("A"); // 0 ✅alternatingCharacters("B"); // 0 ✅alternatingCharacters("AB"); // 0 ✅alternatingCharacters("ABB"); // 1 ✅alternatingCharacters("BBBBB"); // 4 ✅alternatingCharacters("ABABABAB"); // 0 ✅alternatingCharacters("BABABA"); // 0 ✅alternatingCharacters("AAABBB"); // 4 ✅`

# Any Feedback

Web Application / Full Stack JavaScript Developer & Aspiring DevOps

## More from Manny

Web Application / Full Stack JavaScript Developer & Aspiring DevOps