有N个数,例如3351353558,求出最多的那个数

import java.util.ArrayList;    
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 对字符集中字符出现的次数进行排序。
*
* @author 赵学庆 www.java2000.net
*/
public class T {
public static void main(String args[]) {
String str = "12345678hfdjkslahfkj932189oiefsjkar94werfdsf";
Map<Character, KeyValue> map = new HashMap<Character, KeyValue>();
char c;
KeyValue kv = null;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
kv = map.get(c);
if (kv == null) {
kv = new KeyValue();
kv.ch = c;
kv.count = 1;
map.put(c, kv);
} else {
kv.count++;
}
}
List<KeyValue> list = new ArrayList<KeyValue>(map.values());
Collections.sort(list);
for (KeyValue o : list) {
System.out.println(o.ch + "=" + o.count);
}
}
}

class KeyValue implements Comparable {
public int compareTo(Object obj) {
if (obj instanceof KeyValue) {
KeyValue kv = (KeyValue) obj;
return kv.count - this.count;
}
return -1;
}

char ch;

int count;
}

-----------------------------------------------------------------------------------------------------------

public class TestCount
{

/**
* @param args
*/
public static void main(String[] args)
{
String str = "12455547464515475354635";

int[] x = new int[10];
char[] ca = str.toCharArray();
for (int i = 0; i < ca.length; i++)
{
x[ca[i] - '0']++;
}

for (int i = 0; i < x.length; i++)
{
System.out.println("字符" + (char) ('0' + i) + "出现了" + x[i] + "次");
}

}

}

--------------------------------------------------------------------------------------

public static void main(String[] args)
{
int[] data = { 3, 3, 5, 1, 3, 5, 3, 5, 5, 8 };
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
for (int i = 0; i < data.length; i++)
{
if (m.get(data[i]) == null) {
m.put(data[i], 1);
} else {
m.put(data[i], m.get(data[i]) + 1);
}
}
System.out.println("Map里的元素: " + m);

int t = 0;
Set<Map.Entry<Integer, Integer>> set = m.entrySet();
for (Entry<Integer, Integer> entry : set) {
if (entry.getValue() > t) {
t = entry.getValue();
}
}

System.out.println("次数最多的有:");
for (Entry<Integer, Integer> entry : set) {
if (entry.getValue() == t) {
System.out.println(entry.getKey());
}
}
}
}
---------------------------------------------------------------------------------



public static void main(String[] args)
{
String str = "12455547464515475354635";
String out = "";

char[] ca = str.toCharArray();

Arrays.sort(ca);

for (char c : ca)
{
out += c;
}

Matcher m = Pattern.compile("(\\d)\\1*").matcher(out);

while (m.find())
System.out.println(m.group().charAt(0) + "的个数:"
+ m.group().length());
}

----------------------------------------------------------------------


import javax.swing.*; 

public class To {
private static String s = JOptionPane.showInputDialog("Enter a Integer number: ");

public static char getCh(String s) {
int max = 0;
char ch=' ';

for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);

int ntimes = s.length() - s.replaceAll(Character.toString(c), "").length();
if (ntimes > max) {
max = ntimes;
ch = c;
}
}

return ch;
}

public static void main(String[] args){
System.out.println("The number is : "+getCh(s));
}
}
---------------------------------------------------------------------------

import javax.swing.*; 

public class To {
private static String s = JOptionPane.showInputDialog("Enter a Integer number: ");

public static char getCh(String s) {
int max = 0;
char ch=' ';

for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);

int ntimes = s.length() - s.replaceAll(Character.toString(c), "").length();
if (ntimes > max) {
max = ntimes;
ch = c;
}
}

return ch;
}

public static void main(String[] args){
System.out.println("The number is : "+getCh(s));
}
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lz12366/archive/2009/10/05/4633275.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值