10万数组去重,排序,找最多出现次数,(复杂度没有前一个博客好,随手写,有点烂)

package ooDay11.zy13;


import java.util.*;

public class shuzu_02 {
public static void main(String[] args) {
Integer[] array = new Integer[100000];

//System.out.println(array.length);

for (int i = 0; i < array.length; i++) {
array[i] = Random_1000();
}

TreeSet<Integer> tset = new TreeSet<Integer>(Arrays.asList(array));
Iterator i = tset.iterator();
while (i.hasNext()){
System.out.print(i.next() + " ");
}
System.out.println();
System.out.println("------------------------------");
System.out.println("最大次数");
//System.out.println("tset的大小:"+tset.size());



int flag = 0;
//length为重复的数的个数
int lengthCF = tset.size();
//用一个二维数组来存储有的数字和其个数
int[][] array_02 = new int[lengthCF][2];
int num = 0;
for (int j = 0; j < array.length; j++) {
if (tset.contains(array[j])){
flag =0;
if (num == 0){
array_02[num][0] =array[j];
array_02[num][1]=1;
num++;
}else {
for (int k = 0; k < num; k++) {
if (array_02[k][0] == array[j]){
array_02[k][1]++;
flag = 1;
break;
}
}
if (flag == 0){
array_02[num][0] = array[j];
array_02[num][1] = 1;
num++;
}
}
}
}

/*
测试代码
System.out.println("-------------------");
for (int j = 0; j < num; j++) {
System.out.print(array_02[j][0]+"::");
System.out.println(array_02[j][1] + " ");
}
*/

int bo;
//在二维数组中的第二列找出最大数
for (int j = 0; j < num-1; j++) {
for (int k = j+1; k < num; k++) {
if (array_02[k][1]>array_02[j][1]){
bo = array_02[k][1];
array_02[k][1] = array_02[j][1];
array_02[j][1] = bo;

bo = array_02[k][0];
array_02[k][0] = array_02[j][0];
array_02[j][0] = bo;
}
}
}

//查找相同次数,先求最大次数
int max = array_02[0][1];
int count = 0;
for (int j = 0; j < num; j++) {
if (array_02[j][1]==max){
count++;
}
}
//输出
for (int j = 0; j < count; j++) {
System.out.println("薛卫卫找到的最大出现次数的数为"+ array_02[j][0] +" 其出现的次数:" + array_02[j][1]);
}


}


public static int Random_1000(){
int a ;
do {
a = (int) (Math.random()*10000);
}while (a ==0);
return a;
}



}
posted @ 2019-03-15 16:15  小菜鸡的梦想  阅读(464)  评论(1编辑  收藏  举报