/*
*求一列整数中,出现次数最多的那个整数;
*
*/
public class Test
{
public static void main(String args[]){
int[] a = {1,22,22,33,22,33,44,22};
int max = 0;//数的下标 for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length-1;j++){
if(a[i]==a[j]){
//写到这里不会写了,我想法是i每循环一次,相同数字的放到一个数组里面
//或者有其他写法,不吝赐教
}
}
}
System.out.println("出现次数最多的数为:" + a[max]);

}
}

解决方案 »

  1.   

    int[] a = {1,22,22,33,22,33,44,22};
    int max = 0, maxCount;
    Map<String, Integer> map = new HashMap<String, Integer>();
    for (int i = 0; i < a.length; i++) {
    String key = String.valueOf(a[i]);
    Integer count = map.get(key);
    if(count == null){
    map.put(key, new Integer(1));
    } else {
    map.put(key, new Integer(count.intValue() + 1));
    }
    }
    maxCount = map.get(String.valueOf(a[0]));
    for (int i = 1; i < a.length; i++) {
    Integer count = map.get(String.valueOf(a[0]));
    if(count.intValue() > maxCount){
    max = i;
    }
    }
    System.out.println("a[max] = " + a[max]);
      

  2.   

    不好意思,漏了句:
    int[] a = {1,22,22,33,22,33,44,22};
    int max = 0, maxCount;
    Map<String, Integer> map = new HashMap<String, Integer>();
    for (int i = 0; i < a.length; i++) {
    String key = String.valueOf(a[i]);
    Integer count = map.get(key);
    if(count == null){
    map.put(key, new Integer(1));
    } else {
    map.put(key, new Integer(count.intValue() + 1));
    }
    }
    maxCount = map.get(String.valueOf(a[0]));
    for (int i = 1; i < a.length; i++) {
    Integer count = map.get(String.valueOf(a[0]));
    if(count.intValue() > maxCount){
    max = i;
    maxCount = count.intValue();
    }
    }
    System.out.println("a[max] = " + a[max]);
      

  3.   

    int[] a = {0,2,1,22,22,33,22,33,44,22};
    int max = 0, maxCount;
    Map<String, Integer> map = new HashMap<String, Integer>();
    for (int i = 0; i < a.length; i++) {
    String key = String.valueOf(a[i]);
    Integer count = map.get(key);
    if(count == null){
    map.put(key, new Integer(1));
    } else {
    map.put(key, new Integer(count.intValue() + 1));
    }
    }
    maxCount = map.get(String.valueOf(a[0]));
    for (int i = 1; i < a.length; i++) {
    Integer count = map.get(String.valueOf(a[i]));
    if(count.intValue() > maxCount){
    max = i;
    maxCount = count.intValue();
    }
    }
    System.out.println("a[max] = " + a[max]);
      

  4.   

    如果只想找出出现次数最多的整数的第一个数那么可以有如下方法
    (见下面数中1,22,33出现次数均为5次,现在只想取出现次数最多的首先出现的数1就可以用如下方法)
    public class Test
    { int[] a = {1,1,1,1,1,22,22,33,22,33,33,33,44,22,22,33};
    // 出现次数最多的数在数组中的下标
    int index = 0;
    // 出现次数最多的数出现的次数
    int count = 1;
    // 存储当前出现的数出现的次数
    int ncount = 1;
    Arrays.sort(a);
    for(int i=1;i<a.length;i++)
    { if (a[i]==a[i-1])
    {
    ncount++;
    if (ncount>count)
    {
     index = i;
     count = ncount;
    }
    }
    else
    {
    ncount = 1;
    }
    }
    System.out.println("出现次数最多的数为:" + a[index]);
    }
      

  5.   

    思路:
    1. 排序是必要的
    2. 假设排序后数组为2 4 4 4 7 7 7 7 11 11 23 23 34 34 ,先定义四个变量, 一个储存同数字起始位置 start,一个保存相同数字结束位置 end,那么这个数字出现的次数等于前两个变量的相减+1 con,然后用第四个变量保存那个数字 tem。
    3. 循环,当游标位置的数字跟它前面的数字不一致,将end定位到当前位置,相减则得到前一个数字出现的次数,保存到con中,将该数字保存到tem中。然后做一系列修改工作:将当前位置作为start的开始,准备计算下一个数字的出现次数。循环继续,当遇到上一种情况,如上操作。
    4. 当到数组末尾后,判断最后的数字出现的次数跟你保存在tem中数字出现次数con比较,若大则,修改tem和con,否则不操作。
    5. 循环结束,tem和con中,将分别保存出现最多次数的数字和它出现的次数。源程序如下:
    /*
     * 求一列整数中,出现次数最多的那个整数; 
     * 
     * */package test;public class Count {

    /*
     *   排序
     */
    public int[] sort(int[] a) {
    int tem;
    for(int i=0;i<a.length;i++) {
    for(int j=i+1;j<a.length;j++) {
    if(a[i]>a[j]) {
    tem = a[j];
    a[j] = a[i];
    a[i] = tem;
    }
    }
    }
    return a;
    }

    public int counts(int[] a) {
    int start = 0, end = 0;  //  起始位置
    int con = 0;         //  数字最多出现的次数
    int tem = 0;          //  出现最多次数的数字

    for(int i=1;i<a.length;i++) {
    if(a[i-1] != a[i]) {
    end = i;
    if(con < end - start) {
    con = end - start;  //  记住出现次数
    tem = a[i-1];        //  记住这个数字
    }
    start = i;           //  从下一个不重复的数字作为开始


    // 数组末尾
    if(i == a.length - 1) {
    if(a.length -1 - start > con) {
    con = a.length - 1 - start;
    tem = a[a.length-1];
    }
    }
    }
    System.out.println("\n 数字:" + tem + ",次数:" + con);
    return tem;
    }

    public void print(int[] a) {
    System.out.println(" ");
    for(int i=0;i<a.length;i++)
    System.out.print(a[i] + " ");
    }

    public static void main(String args[]) {
    int[] a = {34,7,23,4,2,7,4,23,11,34,7,4,11,7};
    Count c = new Count();
    c.print(a);
    a = c.sort(a);
    c.print(a);
    c.counts(a);
    }
    }结果:
     
    34 7 23 4 2 7 4 23 11 34 7 4 11 7  
    2 4 4 4 7 7 7 7 11 11 23 23 34 34 
     数字:7,次数:4总结:如果用到更高级别的存储结构,可大大方便程序员的工作,但执行效率低多了——比如我定义一个类,保存所有数字和它出现的次数,而后,比较次数大小。但效率不是我们programmer的终极追求么?!
      

  6.   

    String str = "3 2 1 4 5 4 5 3 3 4 5 4";
    String[] arr = str.split("\\s+");
    int[] p = new int[arr.length];
    for(int i=0;i<p.length;i++)p[i]=new Integer(arr[i]).intValue();
    for(int i=0;i<p.length;i++){
    for(int j=i+1;j<p.length;j++){
    if(p[i]>p[j]){
    int mid = p[i];
    p[i] = p[j];
    p[j] = mid;
    }
    }
    }
    for(int i=0;i<p.length;i++)System.out.print(p[i]+" ");
    System.out.println();
    String ret = "";
    int cnt = 0;&#65405;&#12539;
    for(int i=0,j=1;i<p.length-1;i++,j++){
    if(p[i]==p[i+1] && i == p.length-2){
    if(cnt == j+1){
    ret += ","+p[i];
    }else if(cnt == j-1){
    ret = p[i]+"";
    }
    }else if( p[i]!=p[i+1] ){
    if(j>cnt){
    ret = p[i]+"";
    cnt = j;
    }else if(j == cnt){
    ret += ","+p[i];
    }
    j=0;
    }
    }
    System.out.println(ret);参考.
      

  7.   

    用Hashtable,数组中的整数做Key,出现的次数做Valuepublic V put(K key,V value)
    此方法返回:此哈希表中指定键的以前的值;如果不存在该值,则返回 null如果返回不为null,则Value加1至于出现次数最多,遍历Hashtable的就可求出
      

  8.   

    import java.util.HashMap;
    import java.util.Set;
    import java.util.Iterator;
    import java.util.Map;class MaxCount{
    public static void main(String[] args){
    int[] a = {1,1,1,1,1,22,22,33,22,33,33,33,44,22,22,33};

    java.util.HashMap map = new HashMap();

    for(int key : a){

    if(map.containsKey(key)){
    int count = ((Integer)map.get(key)).intValue();
    map.put(key,count+1);
    }else{
    map.put(key,1);
    }
    }

    int maxValue;
    Iterator it = map.values().iterator();
    if(it.hasNext()){
    maxValue = ((Integer)it.next()).intValue();

    while(it.hasNext()){
    int value = ((Integer)it.next()).intValue();
    maxValue = value>maxValue?value:maxValue;
    }
    }else{
    System.out.println ("xxxxxxxx");
    return;
    }


    Set entrys = map.entrySet();
    Iterator itr = entrys.iterator();

    while (itr.hasNext())
    {
    Map.Entry entry = (Map.Entry)itr.next();
    int value = ((Integer)entry.getValue()).intValue();
    if(maxValue == value){
    System.out.print (entry.getKey()+"\t");
    System.out.println ("count="+value+"\t");
    }
    }
    }
    }
      

  9.   

    二叉树难道效率不高吗??SQL中的索引都是树的结构
      

  10.   

    public static void MaxCount(int[] data){
    Arrays.sort(data);
    Map<Integer,Integer> map = new TreeMap<Integer,Integer>();
    for (int i = 0; i < data.length; i++) {
    int count = 0;
    for (int j = 0; j < data.length; j++) {
    if(data[i]==data[j]){
    count++;
    }
    }
    map.put(new Integer(data[i]),new Integer(count));
    }
    Iterator it = map.entrySet().iterator();
    int count = 0;
    int value = 0;
    Set<Integer> set = new HashSet<Integer>();
    while(it.hasNext()){
    Entry e = (Entry)it.next();
    if(count <= (Integer)e.getValue()){
    count = (Integer)e.getValue();
    value = (Integer)e.getKey();
    }
    set.add(value);
    }
    for (Iterator iter = set.iterator(); iter.hasNext();) {
    int key = (Integer)iter.next();
    System.out.println("Value - " + key + "  " + "Count - " + map.get(key));
    }
    }
    public static void main(String[] args) {
    int[] data = {1,2,2,5,2,7,7,1,3,4,1};
    Test5.MaxCount(data);
    }
      

  11.   

    public class Test
    {
    public static void main(String args[]){
    int[] a = {1,22,22,33,22,33,44,22};
    int max = 0;//数的下标
    int count1=0;
    int count2=0;
    for(int i=0;i<a.length;i++){
    for(int j=i+1;j<a.length-1;j++){
    if(a[i]==a[j]) count1++;
    }
    if (count1>count2) {
    max=i;count2=count1;
    }
    count1=0;
    }
    System.out.println("出现次数最多的数为:" + a[max]);}
    }
      

  12.   

    yuanjunqing() ( ) 信誉:100 这位兄弟的比较高。, n*n/2复杂度
      

  13.   

    int[] a = { 1, 1, 1, 1, 1, 22, 22, 33, 22, 33, 33, 33, 44, 22, 22, 33 };
            HashMap<String,Integer> testMap = new HashMap<String,Integer>();
    for (int key : a) {
    if(testMap.containsKey(String.valueOf(key))){
    int count = testMap.get(String.valueOf(key));
    testMap.put(String.valueOf(key), count+1);
    }else{
    testMap.put(String.valueOf(key), 1);
    }
    }
    Object[] sortArray = (Object[])testMap.values().toArray();
    Arrays.sort(sortArray);
    int max=(Integer)sortArray[sortArray.length-1];
    Set entrys = testMap.entrySet();
    Iterator itr = entrys.iterator();
    while (itr.hasNext()) {
    Map.Entry entry = (Map.Entry) itr.next();
    int value = ((Integer) entry.getValue()).intValue();
    if (max == value) {
    System.out.print(entry.getKey() + "\t");
    System.out.println("count=" + value + "\t");
    }
    }