Skip to content

Instantly share code, notes, and snippets.

@d0peCode
Created October 25, 2019 23:37
Show Gist options
  • Save d0peCode/d6e7730c11a316c60bd5f7c0006124f1 to your computer and use it in GitHub Desktop.
Save d0peCode/d6e7730c11a316c60bd5f7c0006124f1 to your computer and use it in GitHub Desktop.
class charactersCounter {
constructor(input) {
this.inputArr = input.split('');
this.availableLetters = [...new Set(this.inputArr)];
this.mostPopular = {
letter: '',
amount: 0
}
}
getOccurrence(arr, val) {
return arr.filter(v => (v === val)).length;
}
addLetterAmount(targetArray) {
for(let i = 0, l = targetArray.length; i < l; i++) {
targetArray[i] = {
letter: targetArray[i],
amount: this.getOccurrence(this.inputArr, targetArray[i])
}
}
}
getMostPopular(fromArray) {
for(let i = 0, l = fromArray.length; i < l; i++) {
if(fromArray[i].amount > this.mostPopular.amount) {
console.log(fromArray[i].amount, this.mostPopular)
this.mostPopular.letter = fromArray[i].letter;
this.mostPopular.amount = fromArray[i].amount;
}
}
}
init() {
this.addLetterAmount(this.availableLetters);
this.getMostPopular(this.availableLetters);
return this.mostPopular.letter;
}
}
const charCounter = new charactersCounter('hello worlddd');
console.log(charCounter.init());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment