java数组元素重复次数最大值

  1. package lianxi;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5.   
  6. public class Demo8  
  7. {  
  8.   
  9.     /** 
  10.      * @file Demo8.java 
  11.      * @author 變脸 
  12.      * @datetime 2016-1-10 上午11:04:37  
  13.      *  
  14.      */  
  15.     public static void main(String[] args)  
  16.     {  
  17.         Object[] obj1   = {"aa","bb","cc","dd","aa","dd","ee","dd","ff","c","dd","rr","aa","aa"};  
  18.         //把数组放进set,去除重复  
  19.         Set<Object> set = new HashSet();  
  20.         for(Object o:obj1)  
  21.         {  
  22.             set.add(o);  
  23.         }  
  24.         //把set里数据放入新的数组obj2[]  
  25.         Object[] obj2 = set.toArray();  
  26.           
  27.         //再做一个数组,存放obj2数组的个数  
  28.         int [] counts = new int[obj2.length];  
  29.           
  30.         //定义数组中元素出现最大次数k  
  31.         int k = 0;  
  32.         //双重循环,对比obj1 obj2  
  33.         for(int i=0;i<obj2.length;i++)  
  34.         {  
  35.             int count=0;  
  36.             for(int j=0;j<obj1.length;j++)  
  37.             {  
  38.                 if(obj2[i]==obj1[j])  
  39.                 {  
  40.                     count++;  
  41.                 }  
  42.             }  
  43.             //如果次数大于最大次数k,则count赋值给k  
  44.             if(count>k)  
  45.                 k=count;  
  46.             //次数存入counts[]  
  47.             counts[i]=count;  
  48.         }  
  49.         System.out.println("最大次数是"+k);  
  50.           
  51.         //最大次数的数可能不止一个,我们用counts[]里面和最大次数相同值的下标,去取obj2[]里面的元素,得到最大次数元素  
  52.         for(int i=0;i<counts.length;i++)  
  53.         {  
  54.             if(counts[i]==k){  
  55.                 System.out.println("出现次数最大的数为:"+obj2[i]);  
  56.             }  
  57.         }  
  58.           
  59.   
  60.     }  
  61.   
  62. }  


Java实现找出数组中重复次数最多的元素以及个数

复制代码
/**数组中元素重复最多的数
     * @param array
     * @author shaobn
     * @param array
     */
    public static void getMethod_4(int[] array){
        Map<Integer, Integer> map = new HashMap<>();
        int count = 0;
        int count_2 = 0;
        int temp = 0;
        for(int i=0;i<array.length;i=i+count){
            if(i==array.length-1){
                temp =1;
                break;
            }
            for(int j=i+1;j<array.length;j++){
                if(array[i]==array[j]){
                    count++;
                }
                continue;
            }
            if(count>count_2){
            count_2 = count;
            map.put(count_2, array[i]);
            }
                        
        }
        System.out.println(map.get(count_2));
    }

int[] array = {1,1,1,5,5,8,9}

     
     
  1. public static void main(String[] args) {  
  2.     String [] arry = {"1","1","1","1","1","2","2","2","2","2","3","3","3","3",};  
  3.     Map<String,Integer> map = new HashMap<String, Integer>();  
  4.     for(int i =0 ;i<arry.length;i++){  
  5.         if(null!= map.get(arry[i])){  
  6.             map.put(arry[i], map.get(arry[i-1])+1); //value+1  
  7.         }else{  
  8.             map.put(arry[i],1);  
  9.         }  
  10.     }  
  11.       
  12.     Iterator it = map.entrySet().iterator();    
  13.     while(it.hasNext()){  
  14.         Map.Entry entry = (Map.Entry) it.next();     
  15.         String  key  =  entry.getKey().toString();        
  16.         int  value  =  Integer.parseInt(entry.getValue().toString());  
  17.         System.out.println("key is :"+key+"---value :"+value);  
  18.     }   
  19. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值