打印出数组中最小的元素发生 [英] Printing out the least occurring elements in an array

查看:115
本文介绍了打印出数组中最小的元素发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我发现这个问题,从数天前,但它是保持状态,它不会让我上张贴任何。

OK, so I found this question from a few days ago but it's on hold and it won't let me post anything on it.

***注:数组中的值或命令是完全随机的。它们还应该能是负的。

***Note: The values or order in the array are completely random. They should also be able to be negative.

有人推荐这个code,并翻阅了起来,但我没有看到该如何解决这个问题。如果发生以上要素之一是不是在数组的开头,然后这不起作用。这是因为MAXCOUNT将等于array.length,结果阵列总是会在下面写的code中的第一个元素。

Someone recommended this code and was thumbed up for it, but I don't see how this can solve the problem. If one of the least occurring elements isn't at the BEGINNING of the array then this does not work. This is because the maxCount will be equal to array.length and the results array will ALWAYS take the first element in the code written below.

有哪些途径来解决这个问题,使用简单的Java如下面?没有哈希地图和诸如此类的东西。我一直在思考了一段时间,但不能真正拿出任何东西。也许使用双数组来存储一定数量的计数?你将如何解决这个问题?任何指导?

What ways are there to combat this, using simple java such as below? No hash-maps and whatnot. I've been thinking about it for a while but can't really come up with anything. Maybe using a double array to store the count of a certain number? How would you solve this? Any guidance?

public static void main(String[] args)
{
    int[] array = { 1, 2, 3, 3, 2, 2, 4, 4, 5, 4 };
    int count = 0;
    int maxCount = 10;
    int[] results = new int[array.length];
    int k = 0; // To keep index in 'results'

    // Initializing 'results', so when printing, elements that -1 are not part of the result
    // If your array also contains negative numbers, change '-1' to another more appropriate
    for (int i = 0; i < results.length; i++) {
        results[i] = -1;
    }

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

        if (count <= maxCount) { // <= so it admits number with the SAME number of occurrences
            maxCount = count;
            results[k++] = array[i]; // Add to 'results' and increase counter 'k'
        }
        count = 0; // Reset 'count'
    }

    // Printing result
    for (int i : results) {
        if (i != -1) {
            System.out.println("Element: " + i + ", Number of occurences: " + maxCount);
        }
    }
}

信贷:<一href=\"http://stackoverflow.com/users/2670792/christian\">http://stackoverflow.com/users/2670792/christian
为code

credit to: http://stackoverflow.com/users/2670792/christian for the code

我不能竖起大拇指,所以我只想在这里说谢谢大家回答人。

I can't thumbs up so I'd just like to say here THANKS EVERYONE WHO ANSWERED.

推荐答案

此算法记录具有出现次数最少的值至今的(因为它的处理),然后打印所有的人一起 MAXCOUNT 的值(也就是具有的的最小出现次数的计数的值)。

This algorithm is recording the values having the least number of occurrences so far (as it's processing) and then printing all of them alongside the value of maxCount (which is the count for the value having the overall smallest number of occurrences).

一个速战速决是记录计数每个位置,然后只打印那些数等于 MAXCOUNT (我已经改名 minCount

A quick fix is to record the count for each position and then only print those whose count is equal to the maxCount (which I've renamed minCount):

public static void main(String[] args) {
    int[] array = { 5, 1, 2, 2, -1, 1, 5, 4 };
    int[] results = new int[array.length];
    int minCount = Integer.MAX_VALUE;
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array.length; j++) {
            if (array[j] == array[i]) {
                results[i]++;
            }
        }
        if (results[i] <= minCount) {
            minCount = results[i];
        }
    }
    for (int i = 0; i < results.length; i++) {
        if (results[i] == minCount) {
            System.out.println("Element: " + i + ", Number of occurences: "
                    + minCount);
        }
    }
}

输出:

Element: 4, Number of occurences: 1
Element: 7, Number of occurences: 1

此版本也颇有几分清洁,并删除了一堆不必要的变数。

This version is also quite a bit cleaner and removes a bunch of unnecessary variables.

这篇关于打印出数组中最小的元素发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆