Remove the repeating characters that appear in a string and calculate the number of occurrences of characters that appear in the

xiaoxiao2021-03-06  43

/ *

* Classical example, remove repeating characters in a string, and calculate the number of occurrences of characters in the most time

* Mainly, the characteristics of each data corresponding to each data in MAP, very cleverly solve this problem

** /

/ *

* This program source code from 9CBS member jfresh_man (the answer is ...), I am in the forum, here, thank you.

* * /

Import java.util. *;

Public class test {

Public static void main (String [] args) {

StringBuffer strbuf = new stringbuffer ("A, B, C, D, E, F, A, A, D, C, C");

StringTokenizer ST = New StringTokenizer (Strbuf.Tostring (), ",")

/ *

* StringTokenizer class is also very good here, because the string to be processed is "," this particular separator

* This just exerts the utility of the StringTokenizer class

* * /

Map set = new hashmap ();

While (st.hasmoretokens ()) {

String Temp = st.nextToken ();

Object o = set.get (TEMP);

IF (o == NULL)

Set.put (Temp, New Integer (1));

Else

Set.put (Temp, New Integer ((Integer O) .intValue () 1));

}

Iterator i = set.keyset (). Iterator ();

While (I.hasNext ()) {

System.out.print (String) i.next () "");

}

SYSTEM.OUT.PRINTLN ();

Integer Integer = (Integer) Collections.max (SET.VALUES ());

/*Collections.max (SET.VALUES ()) method is wonderful, saving a lot of time to compare

* * /

System.out.println (Integer.intValue ());

}

}

Convert the string inverted the output: (like a comma, the space is also reflected in the output)

转载请注明原文地址:https://www.9cbs.com/read-55410.html

New Post(0)