Mega Code Archive

 
Categories / Java / Collections Data Structure
 

Removes duplicate elements from the array

import java.lang.reflect.Array; import java.util.Enumeration; import java.util.Hashtable;      /*********************************************************************      * Array manipulation for Java 1.1+.      *      * <p>      * Java 1.1 compatible.      * </p>      *      * @see      *   ArrayLib2      *      * @version      *   2003-04-07      * @since      *   2001-04-06      * @author      *   <a href="http://croftsoft.com/">David Wallace Croft</a>*/ public class Util{     /*********************************************************************     * Removes duplicate elements from the array.     *********************************************************************/     public static Object [ ]  removeDuplicates ( Object [ ]  array )     //////////////////////////////////////////////////////////////////////     {       Hashtable  hashtable = new Hashtable ( );       for ( int  i = 0; i < array.length; i++ )       {         hashtable.put ( array [ i ], array [ i ] );       }       Object [ ]  newArray = ( Object [ ] ) Array.newInstance (         array.getClass ( ).getComponentType ( ), hashtable.size ( ) );       int  index = 0;       Enumeration  enumeration = hashtable.elements ( );       while ( enumeration.hasMoreElements ( ) )       {         newArray [ index++ ] = enumeration.nextElement ( );       }       return newArray;     } }