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:
|