查找整型数组里出现频率最高的数字的最佳JAVA实现
来源:互联网 发布:乐乎网站 编辑:程序博客网 时间:2024/05/21 20:26
- package test;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
-
-
-
-
-
- public class HelloWindowExample {
- public static void main(String[] args) {
- test1();
- test2();
- testLaoZiZhu();
- testLaoZiZhuCounter();
- testLaoZiZhuCounter2();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void test1() {
- int[] ar = { 5, 5, 8, 5, 3, 5, 3, 3, 3, 1 };
- int[] fr = new int[ar.length];
- int top = 0;
- int i, j;
- for (i = 0; i < fr.length; i++) {
-
- j = i - 1;
- while (j >= 0) {
- if (ar[j] == ar[i]) {
-
- break;
- }
-
- j--;
- }
- if (j < 0) {
-
- fr[i] = 1;
- } else {
-
- fr[i] = fr[j] + 1;
- }
-
- if (fr[i] > top) {
- top = fr[i];
- }
-
- }
- for (i = 0; i < fr.length; i++) {
- if (fr[i] == top)
- System.out.println("" + ar[i] + " " + top);
- }
- }
-
-
-
-
-
-
- public static void test2() {
- String[] array = { "5", "5", "8", "5", "3", "5", "3", "3", "3", "1" };
- List list = Arrays.asList(array);
- list = new ArrayList(list);
- List outValue = new ArrayList();
- int count = 0;
-
- while (list.size() - 1 > 0) {
-
- String element = (String) list.get(list.size() - 1);
-
- int tempCount = 0;
-
- for (int j = list.size() - 1; j >= 0; j--) {
-
-
- if (element.equals(list.get(j))) {
- tempCount++;
- list.remove(j);
- }
- }
-
- if (tempCount > count) {
- count = tempCount;
- outValue.clear();
- outValue.add(element);
- } else if (tempCount == count) {
-
- outValue.add(element);
- }
- }
- System.out.println("出现频率最高的是:" + outValue + "/n出现了:" + count + "次");
- }
-
-
-
-
-
-
-
-
- public static void testLaoZiZhu() {
- int[] ar = { 5, 5, 8, 5, 3, 5, 3, 3, 3, 1 };
- List<Integer> outValue = new ArrayList<Integer>();
- int count = 0;
-
- int has = ar.length;
- int element;
- int tempCount;
- System.out.println(Arrays.toString(ar));
-
- while (has > 0) {
-
- element = ar[has - 1];
-
- tempCount = 0;
-
- for (int j = has - 1; j >= 0; j--) {
-
-
- if (element == ar[j]) {
- tempCount++;
-
-
- ar[j] = ar[has - 1];
- has--;
- }
- }
- System.out.println(Arrays.toString(ar));
-
- if (tempCount > count) {
- count = tempCount;
- outValue.clear();
- outValue.add(element);
- } else if (tempCount == count) {
-
- outValue.add(element);
- }
- }
- System.out.println("出现频率最高的是:" + outValue + "/n出现了:" + count + "次");
- }
-
-
-
-
- public static void testLaoZiZhuCounter() {
- int[] ar = { 5, 5, 8, 5, 3, 5, 3, 3, 3, 1 };
-
- int max = ar[0];
- for (int i = 1; i < ar.length; i++) {
- if (ar[i] > max) {
- max = ar[i];
- }
- }
-
- int[] vote = new int[max + 1];
- int countMax = 0;
-
- for (int i = 0; i < ar.length; i++) {
- vote[ar[i]]++;
- if (vote[ar[i]] > countMax) {
- countMax = vote[ar[i]];
- }
- }
-
- for (int i = 0; i < vote.length; i++) {
- if (vote[i] == countMax) {
- System.out.print(i + ",");
- }
- }
- System.out.println("出现次数为" + countMax);
- }
-
-
-
-
-
- public static void testLaoZiZhuCounter2() {
- int[] ar = { 5, 5, 8, 5, -3, 5, -3, -3, -3, 1 };
-
- int max = ar[0];
- int min = ar[0];
- for (int i = 1; i < ar.length; i++) {
- if (ar[i] > max) {
- max = ar[i];
- } else if (ar[i] < min) {
- min = ar[i];
- }
- }
-
- int[] vote = new int[max - min + 1];
- int countMax = 0;
-
- for (int i = 0; i < ar.length; i++) {
-
- vote[ar[i] - min]++;
- if (vote[ar[i] - min] > countMax) {
- countMax = vote[ar[i] - min];
- }
- }
-
- for (int i = 0; i < vote.length; i++) {
- if (vote[i] == countMax) {
- System.out.print((i + min) + ",");
- }
- }
- System.out.println("出现次数为" + countMax);
- }
- }