Skip to content
Advertisement

Correct result of amount of repeated values of array

I have written this code for detecting repetitive values of an array. But, it shows incorrect results, how to fix it?

JavaScript

Output:

Result

Advertisement

Answer

Firstly, you do not log the result at the right place. Only once the next character has stopped being the same have you found all duplicates (aka. in your else block).

Next, your count starts at 0, so if you want to see r, 2 and you have counted 1 repetition, do repeatedCount + 1.

JavaScript

Note: I have taken the liberty of replacing your else block with a continue in the if block. In my opinion this makes the function easier to understand and ‘flatter’.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement