Removing duplicate characters from a string

This program will remove any duplicate characters from a string. The user will enter a string and the program will then print out a new string with the duplicate characters removed.


public class RemovingDuplicateFromString
{
    public static void main(String[] args)
    {
        String inputString = "";
        String outputString = "";

        Map<Character, Integer> dictionary = new HashMap<Character, Integer>();

        Scanner scanner = new Scanner(System.in);

        System.out.println("Please Enter String:");

        inputString = scanner.nextLine();

        for (int i = 0; i < inputString.length(); i++)
        {
            if (!dictionary.containsKey(inputString.toLowerCase().charAt(i)))
            {
                dictionary.put(inputString.toLowerCase().charAt(i), 1);
                outputString += inputString.charAt(i);
            }//if
        }//for
        System.out.println(outputString);
    }//main
}//class

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s