-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathletterCountI.js
39 lines (33 loc) · 1.14 KB
/
letterCountI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function LetterCount(str){
let arr =str.split(" ")
console.log("array is: ",arr);
let table ={};
for(let i=0; i<arr.length; i++){
let thisWord = arr[i];
table[thisWord]={};
table[thisWord]['highest']=0;
for(let c=0; c<thisWord.length; c++){
let thisCharacter = thisWord[c];
//check if the char exist yet? not-> set count =1, the next times count +=1
table[thisWord][thisCharacter] === undefined?
table[thisWord][thisCharacter]=1:
table[thisWord][thisCharacter]+=1;
if(table[thisWord][thisCharacter] > table[thisWord]['highest']){
table[thisWord]['highest'] = table[thisWord][thisCharacter];
}
}
}
console.log("table is: ",table);
let answer ={
word : null,
count: 1
}
for (let w in table){
if(table[w]["highest"] > answer["count"]){
answer["count"] = table[w]["highest"];
answer["word"] = w;
}
}
return answer["count"]===1? -1: answer["word"];
};
console.log(LetterCount("hello apple pie"));