# Find the Digit Frequency – HackerRank Solution in C One of the frequently asked questions is to find the digit frequency in a string. There are a wide variety of questions that are available in HackerRank related to programming, so this article will be helpful in answering the most commonly asked questions. Consequently, it will be useful for cracking interviews.

• Given a string, consisting of alphabets and digits, find the frequency of each digit in the given string.
##### Input Format

The first line contains a string, which is the given number.

##### Constraints

All the elements are made of English alphabets and digits.

##### Output Format

Print ten space-separated integers in a single line denoting the frequency of each digit.

a11472o5t6

###### Sample Output 0

0 2 1 0 1 1 1 1 0 0

###### Explanation 0

In the given string:
1 occurs two times.
2,4,5,6 and 7 occur one time each.
The remaining digits 0,2,8, and 9 don’t occur at all.

lw4n88j12n1

###### Sample Output 1

0 2 1 0 1 0 0 0 2 0

###### Sample Input 2

1v88886l256338ar0ekk

###### Sample Output 2

1 1 1 2 0 1 2 0 5 0

The solution in C Language :

``````#include<stdio.h>
#include<string.h>

int main ()
{
char a, freq = {0};
int i, n, j, count = 0;
scanf("%[^\n]s", a);    //input from user
n = strlen(a);         // finding string length
char ch = '0';
for (i = 0; i < 10; i++)
{
for (j = 0; j < n; j++)
{
if (a[j] == ch)
{
count++;
}
}
printf("%d ", count);
count = 0;
ch++;
}

return 0;
}
``````

In conclusion , output of above code is shown below :

For more HackerRank solutions you can visit here.

To clarify some important questions which involve the usage of frequency for solving their logic are solved below :

1. Write a C program to remove duplicates from the given String.

``````#include <stdio.h>
void main()
{
char a;
char freq={0};
int len=0,i;
printf("Enter String : ");
scanf("%[^\n]s", a);
for(i=0;a[i]!='\0';i++)
{
len ++;
freq[a[i]]++;
}
for(i=0;i<len;i++)
{
if(freq[a[i]]>1)
{
a[i]='\0';
}
}
for(i=0;i<len;i++)
{
printf("%c",a[i]);
}
}``````

Output :

2. C program to count maximum occurrence in String

``````#include <stdio.h>
#include <string.h>
int main ()
{
char str;
char result;
char freq ={0};
printf("Enter string :");
scanf("%s",str);
int i, max;
int count = 0;
max=0;

for(int i=0;str[i]!='\0';i++)
{
freq[str[i]]++;
}
for(int i=0;str[i]!='\0';i++)
{
if(freq[str[i]]>max)
{
max =freq[str[i]];
result =str[i];
}
}
printf("Maximmum frequency character is %c %d times",result,max);

}``````

Output

3. Program to find the frequency of a particular character.

``````#include <stdio.h>
#include <string.h>
int main()
{
char str,str2,c;
int t=0,count=0,j;
printf("Enter string : ");
fgets(str, sizeof str , stdin);
int i;
printf("Enter character : ");
scanf("%c",&c);
for(i=0;str[i]!='\0';i++)
{

t= (str[i]==c);
if(t==1)
{
count++;
}
}
printf("count is %d",count);
}``````

Output

4. How to remove a duplicate element from an integer array.

``````#include <stdio.h>

int main()
{
int i,j,n;
int a,b;
int freq={0};
printf("Enter size of integer array ");
scanf("%d",&n);
printf("Enter array :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
freq[a[i]]++;
}
for(i=0,j=0;i<n;i++)
{
if(freq[a[i]]<2) //remove duplicate from int array
{
b[j]=a[i];
j++;
}
else
{
b[j]='\0';
}
}
printf("Resultant array is : ");
for(i=0;i<j;i++)
{
printf("%d ",b[i]);
}

}``````

Output :

If you know some other way to write this program then you can leave a comment here or mail us at technonamecontact@gmail.com, So anyone can a get chance to publish their code on our website. As a result, one can improve his knowledge and spread with others

Please check most asked programming questions of strings with examples by clicking here, after that you might get a deep knowledge about string operations, in short, it will be helpful in clearing the interview.

For understanding the most important concept data structures in an easy way you can check this article on our website.

1. Divya Katoch says:
2. chase2learn says: