//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Problem 2.12 on page 43

import schaums.dswj.Arrays;

public class Pr0212
{ private static final int SIZE = 15;
  private static Object[] a = new Object[SIZE];
  
  public static void main(String[] args)
  { schaums.dswj.Arrays.load(a,10,8);
    schaums.dswj.Arrays.print(a);
    Object[] b = withoutDuplicates(a);
    schaums.dswj.Arrays.print(b);
  }
  
  private static Object[] withoutDuplicates(Object[] a)
  { if (a.length<2) return a;  // there are no duplicates
    Object x = a[0];  // use this object as a dummy marker
    for (int i=1; i<a.length-1; i++)
      for (int j=i+1; j<a.length; j++)
        if (a[j].equals(a[i]))
        a[j] = x;
    int count=0;  // count the duplicates
    for (int i=1; i<a.length; i++)
      if (a[i].equals(x)) ++count;
    if (count==0) return a;  // there are no duplicates
    Object[] b = new Object[a.length-count];
    b[0] = x;
    int shift=0;
    for (int i=1; i<a.length; i++)
      if (shift>0 && !a[i].equals(x)) b[i-shift] = a[i];
      else if (a[i].equals(x)) ++shift;
      else b[i] = a[i];
    return b;
  } 
}