/****************************************************************************** * Name: COS 126 staff * NetID: cos126 * Precept: P00 * * Fall 15 Programming Exam 1, part 2 * Count distinct integers entered from standard input. * Input is not sorted. * *****************************************************************************/ public class Count2 { public static void main(String[] args) { int M = Integer.parseInt(args[0]); // no number higher than M - 1 boolean[] b = new boolean[M]; // true means index was input // loop to read input, count integers and fill boolean array int count = 0; while (!StdIn.isEmpty()) { int val = StdIn.readInt(); b[val] = true; count++; } // count distinct numbers int distinct = 0; for (int i = 0; i < M; i++) { if (b[i]) distinct++; } // output StdOut.println(distinct + " distinct values among " + count + " integers"); } }