// 11/20/2019

/** this class checks for duplicate values in an array */
public class Duplicates {
  public static void main(String[] args) {
   
    int[] numbers = {99, 97, 96, 99, 98, 97, 96};
    duplicateSearch(numbers);
    int[] newNums = {1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4};
    duplicateSearch1(newNums);
    
    // print tble of *'s
    for (int i=0; i<5; i++) {
      // outer loop loops for each row
      System.out.print(i + " ");
      for (int j=i; j<5; j++) {
        // inner loop loops on row i to print *'s
        System.out.print("*");
      }
      // finished all *'s on row i
      System.out.println();
    } 
  }
   /** check for duplicates in a SORTED array 
    * @param arr is the array to search
      @return true if duplicate is found
    */
  public static boolean duplicateSearch1(int[] arr) {
    boolean found = false;
    for (int i=0; i<arr.length-1; i++) {
      if (arr[i]==arr[i+1]){
        // don't confuse with arr[i]+1 which adds 1 to value
        // or with: arr[i++]
        // which is the same as arr[i] and then i++
          found = true;
          System.out.println("found a duplicate " + arr[i]);
        }
      } 
    return found;
  }
  /** check for duplicates in an UNsorted array 
    * @param arr is the array to search
      @return true if duplicate is found
    */
  public static boolean duplicateSearch(int[] arr) {
    boolean found = false;
    // outer loop processes each elt at loc i, 
    // inner loop searches the rest of the array for arr[i]
    for (int i=0; i<arr.length-1; i++) {
      for (int j=i+1; j<arr.length; j++) {
        if (arr[i]==arr[j]){
          found = true;
          System.out.println("found a duplicate " + arr[i]);
        }
      } // end for j
    } // end for i
    return found;
  }
}