forked from MainakRepositor/500-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
147.cpp
51 lines (42 loc) · 884 Bytes
/
147.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
// C++ program to count all duplicates
// from string using hashing
#include <iostream>
using namespace std;
# define NO_OF_CHARS 256
class gfg
{
public :
/* Fills count array with
frequency of characters */
void fillCharCounts(char *str, int *count)
{
int i;
for (i = 0; *(str + i); i++)
count[*(str + i)]++;
}
/* Print duplicates present
in the passed string */
void printDups(char *str)
{
// Create an array of size 256 and fill
// count of every character in it
int *count = (int *)calloc(NO_OF_CHARS,
sizeof(int));
fillCharCounts(str, count);
// Print characters having count more than 0
int i;
for (i = 0; i < NO_OF_CHARS; i++)
if(count[i] > 1)
printf("%c, count = %d \n", i, count[i]);
free(count);
}
};
/* Driver code*/
int main()
{
gfg g ;
char str[] = "test string";
g.printDups(str);
//getchar();
return 0;
}