Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save umair-khanzada/70370be5599a77ba2ab8b95b4f64d3a0 to your computer and use it in GitHub Desktop.

Select an option

Save umair-khanzada/70370be5599a77ba2ab8b95b4f64d3a0 to your computer and use it in GitHub Desktop.
// 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