Java Arrays: Finding Unique Numbers In A Group of 10 Inputted Numbers

前端 未结 4 1767
离开以前
离开以前 2021-01-16 05:07

I\'m at a loss here.

I have this homework assignment where I have to enable the user to input 10 numbers, place them in an array, and figure out which inputted numbe

4条回答
  •  礼貌的吻别
    2021-01-16 05:44

    Instead of an array of input values, put them in a Set of Integers. Sets, by definition, store only unique values. If you add 3 'foos', there will be only one 'foo' in the set.

    // Add this to your top-level loop
    Set uniqueValues = new TreeSet;
    uniqueValues.add(number);
    
    // Add this after the loop to write all unique values on one line
    for (Integer value : uniqueValues) {
      System.out.print(value.toString() + " ");
    }
    
    // Now end the line.
    System.out.println();
    

提交回复
热议问题