Created
October 3, 2021 13:17
-
-
Save umair-khanzada/70370be5599a77ba2ab8b95b4f64d3a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Note: The time complexity of this solution is O(n) | |
| function getMaxConsecutiveChar(str) { | |
| let char = preChar = str[0]; | |
| let occurrence = count = 1; | |
| for(let i = 1; i < str.length; i++){ | |
| if(preChar === str[i]) count++; | |
| if(count > occurrence) { | |
| occurrence = count; | |
| char = preChar; | |
| } | |
| if(preChar !== str[i]) { | |
| preChar = str[i]; | |
| count = 1; | |
| } | |
| } | |
| return {char, occurrence} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment