The Artima Developer Community
Sponsored Link

Java Answers Forum
removing duplicates from arrays

3 replies on 1 page. Most recent reply: Nov 21, 2002 9:23 PM by Daniel Ray

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
mary

Posts: 5
Nickname: marymary
Registered: Nov, 2002

removing duplicates from arrays Posted: Nov 20, 2002 8:37 AM
Reply to this message Reply
Advertisement
I have printed out my array of names, and i want to remove the duplicates?? how do I do this?? please help!!


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: removing duplicates from arrays Posted: Nov 20, 2002 11:10 AM
Reply to this message Reply
I would move the array into a List (probably LinkedList) and sort it, then iterate through it, removing the next item whenever it is equal (or identical, depending on what you want) to the current one.

Another easy (easier than the above) and fun trick is to put all the items into a Map (as the keys), then there will of course be no duplicates. There is a thing called a LinkedHashMap which would maintain the original order, by the way.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: removing duplicates from arrays Posted: Nov 20, 2002 2:45 PM
Reply to this message Reply
Actually, come to think of it (I've been using too much Python lately and not enough Java, so I'm forgetting the Java idioms), it would be more sensible to use a Set.

Here is an example that will read a file full of words, and print out all the unique words:
// --- com/gerrans/util/EnumerationAdapterIterator.java: ---
/*                                                               Matt Gerrans
 *                                                                 10/02/2001
 *                                                                 
 * Copyright 2001, Matt Gerrans.
 */
package com.gerrans.util;
 
import java.util.Enumeration;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.lang.UnsupportedOperationException;
 
/**
 * A simple adapter to turn an Enumeration into an Iterator.
 */
public class EnumerationAdapterIterator implements Iterator
{
   Enumeration enumeration;
 
   public EnumerationAdapterIterator( Enumeration e )
   {
      enumeration = e;
   }
 
   public boolean hasNext()
   {
      return enumeration.hasMoreElements();
   }
 
   public Object next() throws NoSuchElementException
   {
      return enumeration.nextElement();
   }
 
   public void remove() throws UnsupportedOperationException
   {
      throw new UnsupportedOperationException();
   }
}
 
// --- SlinginHash.java: ---
import java.io.Reader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
 
import java.util.Set;
import java.util.HashSet;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;
 
import com.gerrans.util.EnumerationAdapterIterator;
 
public class SlinginHash
{
   private Set words;
 
   public SlinginHash( String filename ) throws FileNotFoundException, IOException
   {
      words = new HashSet();
      collectWords(filename);
   }
 
   public void collectWords( String filename ) throws FileNotFoundException, IOException
   {
      StringBuffer text = new StringBuffer();
      Reader reader = new FileReader( filename );
 
      int nextChar = reader.read();
 
      while( nextChar != -1 )
      {
         text.append( (char)nextChar );
         nextChar = reader.read();
      }
 
      String tokens = " \t\n!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~";
      Enumeration tokenizer = new StringTokenizer( text.toString(), tokens );
      EnumerationAdapterIterator data = new EnumerationAdapterIterator(tokenizer);
 
      while( data.hasNext() )
         words.add( data.next() );
   }
 
   public Set getWords()
   {
      return words;
   }
 
   public int getWordCount()
   {
      return words.size();
   }
 
   public static void main( String [] args )
   {      
      if( args.length == 1 )
      {
         String filename = args[0];
 
         try
         {
            SlinginHash sh = new SlinginHash( filename );
            if( sh.getWordCount() > 0 )
            {
               System.out.println( "There are " + sh.getWordCount() +
                                   " unique words in " + filename + " :" );
               
               Iterator word = sh.getWords().iterator();
               while( word.hasNext() )
                  System.out.println( (String)word.next() );
            }
         }
         catch( IOException ioe )
         {
            System.out.println( "Aaack!  --- " + ioe.getMessage() );
         }
      }
   }
}

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: removing duplicates from arrays Posted: Nov 21, 2002 9:23 PM
Reply to this message Reply
I agree. Iterate through the array .. placing the object into a HashMap would be quick and easy. I only want to add that the LinkedHashMap is not available prior to 1.4

Ray.

Flat View: This topic has 3 replies on 1 page
Topic: JAVA Previous Topic   Next Topic Topic: converting html form in a MS Word document

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use