Monday, 9 January 2017

There is a file containing one number in each line. The numbers are in the range from 1 to 1000. Write the pseudocode for a program that finds the non-duplicate numbers (numbers that occur exactly once) and duplicate numbers (numbers that occur more than once) in the file.

class FindDupsAndUnique {
  public static void main(String args[]) {
    Set uniques = new HashSet();
    Set dups = new HashSet();
    
    int[] values= {1,2,2,3,4,5,5};
    for (int i = 0; i < values.length; i++)
      if (!uniques.add(values[i]))
        dups.add(values[i]);

    uniques.removeAll(dups); 

    System.out.println("Unique words:    " + uniques);
    System.out.println("Duplicate words: " + dups);
  }
}

No comments:

Post a Comment