// ITI 1120 // Name: Diana Inkpen, Student# 123456 //import java.io.*; /** * This program checks if an array contains duplicates. * Ex 6.18 in lecture notes */ class CheckDuplicates { public static void main (String[] args) { int [] a; // GIVEN: array of integers boolean result; // RESULT: true if a contains duplicate values // READ IN GIVENS System.out.println( "Please enter the array:" ); a = ITI1120.readIntLine( ); // CALLS THE ALGORITHM result = checkDuplicates(a); // PRINT OUT RESULTS AND MODIFIEDS System.out.println( "The array contains duplicates " + result ); } public static boolean checkDuplicates (int [] a) { int checkIndex; // INTERMEDIATE: index of left comparison value int dupIndex; // INTERMEDIATE: index of right comparison value boolean duplicates = false; // result checkIndex = 0; while ( checkIndex < a.length && ! duplicates ) { dupIndex = checkIndex + 1; while ( dupIndex < a.length && ! duplicates ) { if ( a[checkIndex] == a[dupIndex] ) { duplicates = true; } dupIndex = dupIndex + 1; } checkIndex = checkIndex + 1; } return duplicates; } }