求一列整数中,出现次数最多的那个整数
/* 
 *求一列整数中,出现次数最多的那个整数; 
 * 
 */ 
 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. 排序是必要的 
 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的终极追求么?!