代码没有正确找到Int数组中最常见的元素?

时间:2017-01-11 21:15:31

标签: java arrays

我正在使用此方法,旨在找到数组中最常见的元素。它大部分时间都有效,但由于某些原因它并不总是有效。我还希望它能够存储,如果最常见的是2个数字,但我不确定如何这样做。

This picture shows the issue

以下是该方法的代码:(变量大小是数组的大小)

public static int mostCommon(int size) {

        int mostCommon = 0, mostCommonCount = 0, currentCount = 0;

        for (int i = 1; i < size; i++) {

            if (array[i - 1] == array[i]) {
                currentCount++;

                if (currentCount > mostCommonCount) {
                    mostCommonCount = currentCount;
                    mostCommon = array[i];
                }
            }
            else 
                currentCount = 0;

        }

        return mostCommon;
    }

此代码位于main中,并打印出最常见的元素:

if (mostCommon(size) == 0)
            System.out.println("\nAll Elements In Your Array Occur Equally");
        else 
            System.out.println("\nThe Most Common Element In Your Array Is: " + mostCommon(size));

我真的很感激帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

现在猜测这已经不重要了,但我想我还是会回答的。

我不完全理解为什么要传入数组的大小,而不传入数组本身,无论如何,我有一个解决方案,从您的方法签名中稍作修改,但是如果仍然需要,可以将其修改为适合您的实际情况。

public static Set<Integer> mostCommon()
    {
        int[] array = new int[] {1,2,3,4,5,5,4,3,4};
        Map<Integer, Integer> counts = new HashMap<Integer,Integer>();

        Set<Integer> highestCount = new TreeSet<Integer>();

        //loop through the array to count common values
        for(int numInArray : array)
        {
            //if number in array already been seen
            if(counts.containsKey(numInArray))
            {
                counts.put(numInArray, counts.get(numInArray)+1);
            }
            //else set it at one
            else
            {
                counts.put(numInArray, 1);
            }
        }   


        //loop through map to count highest occurences
        int maxValue = 0;
        int maxKey = 0;
        for(Integer mapKey : counts.keySet())
        {
            int value = counts.get(mapKey);
            //if value is greater than maxValue then set maxVale=value, also clear highestCount as they are lower now
            if(value > maxValue)
            {
                highestCount.clear();
                maxValue = value;
                maxKey = mapKey;                
            }
            //if value is the same as maxValue then store it in list, this will allow us to get two of the same max occurences
            else if(value == maxValue)
            {

                highestCount.add(mapKey);
            }
        }
        highestCount.add(maxKey);

        System.out.println("counts " + counts);
        System.out.println("final answer " + highestCount);

        return highestCount;
    }

我已经测试了数组中的各种值,并且似乎对我尝试过的所有方法都有效。

这绝不是最有效的方法,它只是可行的解决方案。

编辑:看到了另一个问题以及您发布的代码就是这种方法,您的代码比这个答案要好得多

答案 1 :(得分:0)

您可以通过此解决方案获得逻辑,而用于解决此问题的语言是SWIFT 4.2

var arrColor = ["red","green","blue","green","red","green","blue","green","red","green","blue","green","blue","green","red","green","blue","blue","green","red","green","blue","blue","blue","blue","blue"]

func mostCommonArray(array:[String])->[String]{

    var commonArr = [String]()
    var dictColor = [String:Int]()

    for color in array{
        if let count = dictColor[color]{
            dictColor[color] = count + 1
        }
        else{
            dictColor[color] = 1
        }
    }
        let highestValue = dictColor.values.max()

        for (color,count) in dictColor{
            if dictColor[color] == highestValue{
                commonArr.append(color)
            }

        }


    return commonArr
}