remove duplicates

package algorithm.codingpractice;

public class RemoveDuplicates
{

    public static void removeDups( char[] str)
    {
        if( str == null ) return;
        if( str.length == 1 ) return;
        int tail = 1;
        for( int i = 1; i < str.length; ++i )
        {
            int j;
            for( j = 0; j < tail; ++j )
            {
                if( str[i] == str[j] )
                {
                    str[j] = '0';
                    break;
                }
            }
            if( j == tail )
            {
        //        str[tail] = str[i];
                ++tail;
            }
        }
    }
    /**
     * @param args
     */
    public static void main( String[] args )
    {
        char[] chars = {'a', 'b', 'c', 'c', 'b', 'd', 'e'};
        removeDups( chars );
        for( int i = 0; i < chars.length; i++ )
        {
            System.out.print(chars[i] + " "); //$NON-NLS-1$
        }
    }

}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s