+ 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
4 Answers
+ 2
Mohammed No. It should merge only consecutive (where next element is same as current) elements.
See description for expected result.
+ 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)
+ 1
The solution is as follows:
https://code.sololearn.com/WGV9q9TZB3oO/?ref=app
0
Do you mean the expected result of the first array is:
[aa, bb, cc, dd, ee] ?