+ 2

[SOLVED] Count And Remove Consecutive Duplicates From An Array

I want to delete the consecutive duplicate values from the array and count unique consecutive values. But I am not getting expected result. Please check and help. Current Output: [aa, bb, aa, cc, cc, dd, ee, cc] [1, 1, 1, 2, 2, 2, 1, 1] Expected Output: [aa, bb, aa, cc, dd, ee, cc] [1, 1, 1, 4, 2, 1, 1] https://code.sololearn.com/W69Q4IqextAQ/?ref=app

26th Jan 2022, 5:23 PM
Bibek Oli
Bibek Oli - avatar
4 Respostas
+ 2
Mohammed No. It should merge only consecutive (where next element is same as current) elements. See description for expected result.
26th Jan 2022, 5:48 PM
Bibek Oli
Bibek Oli - avatar
+ 1
In if part, increment count and dont push any. In else part push all and reset count.. edit : wordArray = [ "aa", "bb", "aa", "cc", "cc", "cc", "cc", "dd", "dd", "ee", "cc" ] newArray = []; countArray = []; for (i = 0, count=1; i<wordArray.length; i++) { //..removed if(wordArray[i] === wordArray[i + 1]) { count = count + 1; //..removed } else { newArray.push(wordArray[i]); countArray.push(count); count = 1; } } console.log(newArray) console.log(countArray)
26th Jan 2022, 6:09 PM
Jayakrishna 🇮🇳
26th Jan 2022, 6:11 PM
JaScript
JaScript - avatar
0
Do you mean the expected result of the first array is: [aa, bb, cc, dd, ee] ?
26th Jan 2022, 5:45 PM
Mohammed