The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000

Advertisement

Advertisement
where you can ask and answer Java programming questions.">
artima.com - a resource for Java and Jini developers
Artima | Search | Java | Design | JVM | Jini | Books | Seminars | Subscribe 


Java Answers Forum
December 2000

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Without using hashtable...

Posted by Kishori Sharan on December 05, 2000 at 10:54 PM

Following is the program which gets you the character from a string which occurs maximum no. of times in that string. However, you cannot get the second maximum and or anything else using this style.

Thanx
Kishori

///////////////Test.java

public class Test {

public static void main ( String[] args ) {
String str = "THISISATESTSTRING" ;
int total = str.length ( ) ;
char maxChar = 0 ;
char tempChar = 0 ;
int maxTime = 0 ;
int tempMax = 0 ;

for ( int i = 0 ; i < total ; i++ ) {
tempChar = str.charAt ( i ) ;
tempMax = 1 ;
for ( int j = i + 1 ; j < total ; j++ ) {
if ( str.charAt ( j ) == tempChar ) {
tempMax++ ;
}
}

if ( tempMax > maxTime ) {
maxTime = tempMax ;
maxChar = tempChar ;
}
}

System.out.println ( "Character = \'" + maxChar + "\'\n Maximum no. of times = " + maxTime ) ;
}

}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us