在具有1000个元素的整数数组中查找多个模式

时间:2014-01-16 01:35:40

标签: java arrays integer mode

所以我需要一种方法来在1000个元素的数组中找到模式,每个元素使用0-300的math.Random()随机生成。

int[] nums = new int[1000];

    for(int counter = 0; counter < nums.length; counter++)
        nums[counter] = (int)(Math.random()*300);

int maxKey = 0;
    int maxCounts = 0;

    sortData(array);
    int[] counts = new int[301];

    for (int i = 0; i < array.length; i++)
    {
        counts[array[i]]++;
        if (maxCounts < counts[array[i]]) 
        {
            maxCounts = counts[array[i]];
            maxKey = array[i];
        }
    }

这是我当前的方法,它给出了最多的数字,但如果事实证明其他事情发生的次数相同,则只输出一个数字而忽略其余数字。

我们不允许使用ARRAYLIST或HASHMAP(老师禁止)

请帮我详细说明如何修改此代码以生成包含随机数组中所有模式的数组输出。

谢谢你们!

编辑:

感谢你们,我明白了:

private static String calcMode(int[] array)
{
    int[] counts = new int[array.length];
    for (int i = 0; i < array.length; i++) {
        counts[array[i]]++;
    }
    int max = counts[0];
    for (int counter = 1; counter < counts.length; counter++) {
        if (counts[counter] > max) {
            max = counts[counter];
        }
    }

    int[] modes = new int[array.length];

    int j = 0;
    for (int i = 0; i < counts.length; i++) {
        if (counts[i] == max)
            modes[j++] = array[i];
    }

    toString(modes);
    return "";
}

public static void toString(int[] array)
{
    System.out.print("{");
    for(int element: array)
    {
        if(element > 0)
            System.out.print(element + " ");
    }
    System.out.print("}");
}

4 个答案:

答案 0 :(得分:2)

看看这个,没有经过全面测试。但我认为它实现了@ajb所说的:

private static int[] computeModes(int[] array)
{
    int[] counts = new int[array.length];
    for (int i = 0; i < array.length; i++) {
        counts[array[i]]++;
    }
    int max = counts[0];
    for (int counter = 1; counter < counts.length; counter++) {
        if (counts[counter] > max) {
            max = counts[counter];
        }
    }

    int[] modes = new int[array.length];

    int j = 0;
    for (int i = 0; i < counts.length; i++) {
        if (counts[i] == max)
            modes[j++] = array[i];
    }

    return modes;
}

这将返回带有模式的数组int[]。它将包含大量0 s,因为结果数组(modes[])必须使用与传递的数组相同的长度进行初始化。因为每个元素可能只出现一次。

使用main方法调用时:

public static void main(String args[])
{
    int[] nums = new int[300];

    for (int counter = 0; counter < nums.length; counter++)
        nums[counter] = (int) (Math.random() * 300);

    int[] modes = computeModes(nums);
    for (int i : modes)
        if (i != 0) // Discard 0's
            System.out.println(i);
}

答案 1 :(得分:0)

您的第一种方法很有希望,您可以按如下方式扩展它:

for (int i = 0; i < array.length; i++)
{
    counts[array[i]]++;
    if (maxCounts < counts[array[i]]) 
    {
        maxCounts = counts[array[i]];
        maxKey = array[i];
    }
}

// Now counts holds the number of occurrences of any number x in counts[x]
// We want to find all modes: all x such that counts[x] == maxCounts

// First, we have to determine how many modes there are
int nModes = 0;

for (int i = 0; i < counts.length; i++)
{
    // increase nModes if counts[i] == maxCounts
}

// Now we can create an array that has an entry for every mode:
int[] result = new int[nModes];

// And then fill it with all modes, e.g:
int modeCounter = 0;
for (int i = 0; i < counts.length; i++)
{
    // if this is a mode, set result[modeCounter] = i and increase modeCounter  
}

return result;

答案 2 :(得分:0)

这是使用ARRAYLIST ,但我认为我应该回答这个问题,以便您可以使用我的思考过程并自行删除ArrayList用法。那,这可以帮助另一位观众。

这是我想出来的东西。我真的没有解释,但我不妨分享我的进展:

获取int数组并返回没有重复的int数组的方法:

public static int[] noDups(int[] myArray) 
{ 
    // create an Integer list for adding the unique numbers to
    List<Integer> list = new ArrayList<Integer>();

    list.add(myArray[0]); // first number in array will always be first
    // number in list (loop starts at second number)

    for (int i = 1; i < myArray.length; i++)
    {
        // if number in array after current number in array is different
        if (myArray[i] != myArray[i - 1]) 
            list.add(myArray[i]); // add it to the list
    }

    int[] returnArr = new int[list.size()]; // create the final return array
    int count = 0;

    for (int x : list) // for every Integer in the list of unique numbers
    {
        returnArr[count] = list.get(count); // add the list value to the array
        count++; // move to the next element in the list and array
    }

    return returnArr; // return the ordered, unique array
}

查找模式的方法:

public static String findMode(int[] intSet)
{
    Arrays.sort(intSet); // needs to be sorted

    int[] noDupSet = noDups(intSet);
    int[] modePositions = new int[noDupSet.length];

    String modes = "modes: no modes."; boolean isMode = false;

    int pos = 0;
    for (int i = 0; i < intSet.length-1; i++)
    {
        if (intSet[i] != intSet[i + 1]) {
            modePositions[pos]++;
            pos++;
        }
        else {
            modePositions[pos]++;
        }
    }
    modePositions[pos]++;

    for (int modeNum = 0; modeNum < modePositions.length; modeNum++)
    {
        if (modePositions[modeNum] > 1 && modePositions[modeNum] != intSet.length)
            isMode = true;
    }

    List<Integer> MODES = new ArrayList<Integer>();

    int maxModePos = 0;
    if (isMode) {
        for (int i = 0; i< modePositions.length;i++)
        {
            if (modePositions[maxModePos] < modePositions[i]) {
                maxModePos = i;
            }
        }

        MODES.add(maxModePos);
        for (int i = 0; i < modePositions.length;i++)
        {
            if (modePositions[i] == modePositions[maxModePos] && i != maxModePos)
                MODES.add(i);
        }

        // THIS LIMITS THERE TO BE ONLY TWO MODES
        // TAKE THIS IF STATEMENT OUT IF YOU WANT MORE         
        if (MODES.size() > 2) {
            modes = "modes: no modes.";
        }
        else {
            modes = "mode(s): ";
            for (int m : MODES)
            {
                modes += noDupSet[m] + ", ";
            }
        }
    }
    return modes.substring(0,modes.length() - 2);
}

测试方法:

public static void main(String args[]) 
{
    int[] set = {4, 4, 5, 4, 3, 3, 3};
    int[] set2 = {4, 4, 5, 4, 3, 3};
    System.out.println(findMode(set)); // mode(s): 3, 4
    System.out.println(findMode(set2)); // mode(s): 4
}

答案 3 :(得分:0)

构造模式数组的最后部分存在逻辑错误。原始代码为modes[j++] = array[i];。相反,它应该是modes[j++] = i。换句话说,我们需要将该数字添加到其出现次数等于最大出现次数

的模式中