-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxStringCharacter.html
More file actions
54 lines (49 loc) · 1.36 KB
/
Copy pathmaxStringCharacter.html
File metadata and controls
54 lines (49 loc) · 1.36 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Maximum string character repeated</h1>
<script>
// let str="hello peter ooo";
// let strObj={};
// let maxKey='';
// for(let i=0;i<str.length;i++){
// // console.warn(str[i])
// let key=str[i];
// if(!strObj[key]){
// strObj[key]=0;
// };
// strObj[key]++;
// if(maxKey == '' || strObj[key]>strObj[maxKey] )
// {
// maxKey=key;
// }
// }
// console.warn(maxKey)
// console.warn(strObj)
function maxStringCharacter(str) {
let maxCount = 0;
let maxChar = "";
let charCount = {};
for (let i = 0; i < str.length; i++) {
let char = str[i];
if (charCount[char]) {
charCount[char]++;
} else {
charCount[char] = 1;
}
if (charCount[char] > maxCount) {
maxCount = charCount[char];
maxChar = char;
}
}
return `Maximum character is ${maxChar} and it is repeated ${maxCount} times`;
}
console.log(maxStringCharacter("Pradddddkash"));
</script>
</body>
</html>