Find the Digit Frequency – HackerRank Solution in C

Hackerrank solution - Digit Frequency

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.

Sample Input 0

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.

Sample Input 1

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[1000], freq[256] = {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 :

 find the digit frequency in a string.

“The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct.”

― Donald E. Knuth, Selected Papers on Computer Science

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[100];
    char freq[256]={0};
    int len=0,i;
    printf("Enter String : ");
    scanf("%[^\n]s", a);
     printf("Answer is : ");
    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 :

program to remove duplicate from given String.

2. C program to count maximum occurrence in String

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[100];
  char result;
  char freq[256] ={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

 program to count maximum occurrence in String

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

#include <stdio.h>
#include <string.h>
int main()
{
    char str[100],str2[100],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

program to find the frequency of a particular character.

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

#include <stdio.h>


int main()
{
   int i,j,n;
  int a[100],b[100];
  int freq[100]={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 :

 program to remove duplicate from integer array

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.

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

You can also follow us on Twitter for daily updates.

One response to “Find the Digit Frequency – HackerRank Solution in C”

Leave a Reply

Your email address will not be published.