forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_count.cpp
83 lines (56 loc) · 1.79 KB
/
max_count.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Program to get the maximum occurring character in a string.
Steps:-
1.String to be entered by user.
2.Initializing the string, an output variable to store the maximum occured character, getmax .
3.Calculate the string length by using strlen function.
4.Using a for loop it will do the comparison to find out the maximum occured character.
5.If character count==1, it will give no character repeated else it will print the character repeated.
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{ char str[256];
//creating an array namely count to keep track of individual count of characters.
//here used 256 as we have total 256 ASCII characters.
int count[256]={0};
// 'output' will store the maximum occuring character in the string.
int output;
int getmax=0;
cout<<"Enter the String:-\n";
cin>>str;
//this 'strlen' will calculate the length of the string
int length=strlen(str);
//it will give the maximum occured character
for(int i=0;i<length;i++)
{
count[str[i]]++;
if (getmax < count[str[i]])
{
getmax = count[str[i]];
output = int(str[i]);
}
}
//if no character repeated i.e. count is 1.
if(count[output]==1)
{
cout<<"No duplicate exists";
}
//it will print the maximum occured character
else
cout<<char(output);
}
/* Testcases:
1.Input:
Enter the String:-
acbd
Output:
No duplicate exists
2.Input:
Enter the String:-
alliswellattheend
Output:
l
Complexity:
Time Complexity-O(n)
Space Complexity-0(1)
*/