Distinct.java


Below is the syntax highlighted version of Distinct.java.


/************************************************************************
  * * Compile: javac Distinct.java
  * Execute: java Distinct int0 int1 int2 ...
  *
  * Input: a list of integers
  * Output: true if the inputs all have different values, false otherwise
  *
  * % java Distinct 11 23 -7 0 99 5 42
  * true
  *
  * % java Distinct 2 4 6 3 6
  * false
  *
  * % java Distinct -3 -2 -1 -0 +3 +2 +1 +0
  * false 
  * (reason: -0 is equal to +0, when considered as integers)
  ***********************************************************************/

public class Distinct {
    public static void main(String[] args) {
        
        int N = args.length;
        
        // convert each arg and store them in an array of integers
        int[] values = new int[N];
        for (int i = 0; i < N; i++)
            values[i] = Integer.parseInt(args[i]);
        
        // are all of the pairs examined so far distinct?
        boolean result = true;
        
        // we'll examine each values[i] in the array
        for (int i = 0; i < N; i++) {
            // we'll examine values[j] for each other j
            for (int j = i+1; j < N ; j++) {
                // are they different or not?
                if (values[i] == values[j]) {
                    result = false;
                }
            }
        }
        
        System.out.println(result);
    }
}