计算数组中整数的出现次数

时间:2014-10-25 19:54:25

标签: java loops

经过数小时的研究,我找不到我要找的答案,所以我谦虚地向这个伟大论坛的人们询问完美答案。

注意:没有映射,没有排序

这是我的代码:

   public static void countArray(int[] n){
        int[] m = new int[n.length]; //50 elements of integers between values of 10 & 20
        int count = 0;
        int sum = 0;

        for ( int i = 0; i < n.length ; i++){
            m[i] = n[i]; //make a copy of array 'n'
            System.out.print(m[i]+" ");

        }System.out.println();

        for ( int j =0; j < n.length ; j++){
            count =0;
            for(int i = 0; i < n.length ; i++){
                if (n[j]%m[i]==0 && n[j] == m[i])
                    count++;
            }if ( n[j]%m[j] == 0)
            System.out.println(m[j] + " occurs = " + count);
        }   
    }

所以问题是:我在不同的行上得到重复的结果,如:“25 = = 5”。

我的想法:问题出现是因为if ( n[j]%m[j] == 0) 所以我尝试了if ( n[j]%m[j+1] == 0)。出现了另一个问题,因为m[j]将是m[50]所以它会崩溃,但会给我一些我想要的结果。

我想要的结果:类似这样的事情:没有重复并覆盖集合中的所有随机整数

17 occurs = 3
23 occurs = 2
19 occurs = 3
15 occurs = 2
12 occurs = 2

我感谢所有积极和消极的评论。 P.S新的Java编程

4 个答案:

答案 0 :(得分:0)

试试这个:(对数组进行排序然后计算元素的出现次数)

public static void countArray(int[] n) {
    int count = 0;
    int i, j, t;
    for (i = 0; i < n.length - 1; i++) // sort the array
    {
        for (j = i + 1; j < n.length; j++) {
            if (n[i] > n[j]) {
                t = n[i];
                n[i] = n[j];
                n[j] = t;
            }

        }
    }

    for (i = 0; i < n.length;)
    {
        for (j = i; j < n.length; j++) {
            if (n[i] == n[j])
            {
                count++;
            } else
                break;
        }
        System.out.println(n[i] + " occurs " + count);
        count = 0;
        i = j;

    }

}

答案 1 :(得分:0)

通过一些调整,您的代码应该可以工作:

public static void countArray(int[] n){
    boolean [] alreadyCounted = new boolean[n.length]; 

    for (int i = 0; i < n.length ; i++){
        int count = 0;
        if (alreadyCounted[i]) {
            // skip this one, already counted
            continue;
        }
        for(int j = 0; j < n.length ; j++){
            if (n[i] == n[j]) {
                // mark as already counted
                alreadyCounted[j] = true;
                count++;
            }
        }
        System.out.println(n[i] + " occurs = " + count);
    }   
}

你肯定可以使用相同的逻辑和更好的代码,我只是试图遵循原始的“编码风格”;

这是O(n ^ 2)解决方案(读取“非常慢”)。
如果你可以使用排序,你可以在O(n log(n))中进行 - 即快速
使用映射,您可以在O(n)中执行 - 即非常快;

答案 2 :(得分:0)

如果利用输入限制,可能会丢失嵌套循环:

public static void main(String[] args)
{
    //6 elements of integers between values of 10 & 20
    int[] countMe = { 10, 10, 20, 10, 20, 15 };

    countArray(countMe);
}

/** Count integers between values of 10 & 20 (inclusive) */
public static void countArray(int[] input)
{
    final int LOWEST = 10;
    final int HIGHEST = 20;

    //Will allow indexes from 0 to 20 but only using 10 to 20
    int[] count = new int[HIGHEST + 1]; 

    for(int i = 0; i < input.length; i++)
    {
        //Complain properly if given bad input
        if (input[i] < LOWEST || HIGHEST < input[i])
        {
            throw new IllegalArgumentException("All integers must be between " +
                    LOWEST + " and " + HIGHEST + ", inclusive");
        }

        //count
        int numberFound = input[i]; 
        count[numberFound] += 1;
    }

    for(int i = LOWEST; i <= HIGHEST; i++)
    {
        if (count[i] != 0) 
        {
            System.out.println(i + " occurs = " + count[i]);
        }
    }
}   

答案 3 :(得分:0)

这是一种很好,有效的方法,比这里发布的其他解决方案更有效。这个在O(n)时间运行,其中数组的长度为n。它假定您有一些数字MAX_VAL,表示您可能在数组中找到的最大值,并且最小值为0.在您的评论中,您建议使用MAX_VAL==20

public static void countOccurrences(int[] arr) {
    int[] counts = new int[MAX_VAL+1];
    //first we work out the count for each one
    for (int i: arr)
        counts[i]++;
    //now we print the results
    for (int i: arr)
        if (counts[i]>0) {
            System.out.println(i+" occurs "+counts[i]+" times");
            //now set this count to zero so we won't get duplicates
            counts[i]=0;
        }
}

它首先遍历数组,每次找到一个元素时都会增加相关的计数器。然后它返回,并打印出每个的计数。但是,至关重要的是,每次打印整数的计数时,它会将该计数重置为0,这样就不会再次打印。

如果你不喜欢for (int i: arr)风格,这完全等同于:

public static void countOccurrences(int[] arr) {
    int[] counts = new int[MAX_VAL+1];
    //first we work out the count for each one
    for (int i=0; i<arr.length; i++)
        counts[arr[i]]++;
    //now we print the results
    for (int i=0; i<arr.length; i++)
        if (counts[arr[i]]>0) {
            System.out.println(arr[i]+" occurs "+counts[arr[i]]+" times");
            //now set this count to zero so we won't get duplicates
            counts[arr[i]]=0;
        }
}