如何避免重复相同的数字?

时间:2012-04-06 19:53:20

标签: java arrays function for-loop

这就是我想要的:
让用户输入任意数量的数字,直到输入非数字为止(您可以 假设将少于100个数字)。找到最常输入的号码。 (如果 不止一个,打印所有这些。)
示例输出:
输入:5
输入:4
输入:9
输入:9
输入:4
输入:1
输入:a 最常见的是:4,9 我已经到了我的代码中,我已经设法找出哪些是最常见的数字。但是,我不想一遍又一遍地打印出相同的号码;上面的例子:最常见的:4,9,9,4 需要做什么?

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] input = new String[100];
    System.out.print("Input: ");
    input[0] = in.readLine();
    int size = 0;
    for (int i = 1; i < 100 && isNumeric(input[i-1]); i++) {
            System.out.print("Input: ");
            input[i] = in.readLine();
            size = size + 1;
    }
    /*for (int i = 0; i < size; i++) { //testing
        System.out.println(input[i]);
    }*/
    int numOccur;
    int[] occur = new int[size];
    for(int i = 0; i < size; i++) {
        numOccur = 0;
        for (int j = 0; j < size; j++) {
            if(input[i].equals(input[j])) {
                numOccur = numOccur + 1;
            }
        }
        occur[i] = numOccur;
        //System.out.println(numOccur); //testing
    }
    int maxOccur = 0;
    for(int i = 0; i < size; i++) {
        if(occur[i] > maxOccur) {
            maxOccur = occur[i];
        }
    }
    //System.out.println(maxOccur); //testing
    for (int i = 0; i < size && !numFound; i++) {
        if(occur[i] == maxOccur) {
           System.out.println(input[i]);
        }
    }

}

//checks if s is an in, true if it is an int
public static boolean isNumeric (String s) {
    try {
        Integer.parseInt(s);
        return true; //parse was successful
    } catch (NumberFormatException nfe) {
        return false;
    }
}

找到了解决方案!

String[] mostCommon = new String[size];
    int numMostCommon = 0;
    boolean numFound = false;
    for (int i = 0; i < size; i++) {
        int isDifferent = 0;
        if (occur[i] == maxOccur) {
            for (int j = 0; j < size; j++) {
                if (!(input[i].equals(mostCommon[j]))) {
                    isDifferent = isDifferent + 1;
                }
            }
            if (isDifferent == size) {
                mostCommon[numMostCommon] = input[i];
                numMostCommon = numMostCommon + 1;
            }
        }
    }
    for (int i = 0; i < numMostCommon - 1; i++) {
        System.out.print("Most common: " + mostCommon[i] + ", ");
    }
    System.out.println(mostCommon[numMostCommon - 1]);

4 个答案:

答案 0 :(得分:1)

您可以使用哈希表来存储频率,因为限制非常少,即小于100。 伪代码就像:
vector<int> hash(101)
    cin>>input
    if(isnumeric(input))
       hash[input]++
   else{
         max=max_element(hash.begin(),hash.end());
         for(int i=0;i<100;i++)
            if(hash[i]==max)
                print i
   }

答案 1 :(得分:0)

您可以使用Set并存储已打印的值。

答案 2 :(得分:0)

    Set<Integer> uniqueMaxOccur = new HashSet<Integer>();  
    for (int i = 0; i < size ; i++) {
        if(occur[i] == maxOccur) {
            //System.out.println(input[i]);
            uniqueMaxOccur.add(input[i]);
        }
    }

并显示集合

中的值

答案 3 :(得分:0)

这样的事情怎么样?

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    Map<string,int> numberLookup = new HashMap<string,int>();
    Boolean doContinue = true;
    while (doContinue)
    {
        System.out.print("Input: ");
        String input = in.readLine();
        if (isNumeric(input))
        {
            if (!numberLookup.containsKey(input))
                numberLookup.put(input,1);
            else
                numberLookup.put(input, numberLookup.get(input) + 1);
        }
        else
            doContinue = false;
    }

    maxOccur = numberLookup.values().max();
    System.out.print("These numbers were all entered " + maxOccur + " times:");
    Iterator it = numberLookup.entrySet().iterator();
    while (it.hasNext())
    {
        (Map.Entry)it.next();
        System.out.println(pairs.getKey());
    } 
}

抱歉,我是C#的人,并且没有Java编译器,所以这可能需要一些调整。