GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Concepts  >  Data Structures

 Print  |  
Question:  Data Structure - character that repeats itself

Answer: Given a string of characters (let us say there are about 100 characters or more in the string), what is the most efficient method to use for finding out the character that repeats itself the most?Is it possible for us to use some kind of data structure for this?, if this is so,which is the best one that needs to be used?


February 02, 2008 03:26:01 #3
 contact.nidhigupta   Member Since: January 2008    Total Comments: 3 

RE: Data Structure - character that repeats itself
 
Key points are:
1) Max number of characters can only be 256.
2) There is need to specify only the character repeating max no. of times.

so i think one simple solution can be:
 

#include <stdio.h>

#include<stdlib.h>

#define MAXNUMCHAR 256
void main()

{

int i=0;

char s[100];

char ascii = 0;

int counter[MAXNUMCHAR] = {0};

char max = 0;

int maxNum = 0;printf(

"nEnter the string : ");

gets(s);

printf("String = %sn",s);for(i=0; s[i] != ''; i++)

{

ascii = s[i];

counter[ascii]++;

if (counter[ascii] > max) {

max = counter[ascii];

maxNum = ascii;

}

}

printf(" %c is max repeated letter for %d timesn", maxNum, max);

_getch();

}

     

 

Back To Question