Checking for unique charecters in a String using Java

import java.util.HashMap;

//If no addition data structures can be used then we need to comapare each and every charecter with

//all the charectersi in a string

//We can also use the boolean array to check for duplicate charesters in the string

public class uniquestr {

public static void main(String [] args){

String s = “Ishan”;

char[] c = s.toCharArray();

HashMap< Character, Integer> map = new HashMap<Character, Integer>();

for(char i : c){

if(map.containsKey(i))

{

map.put(i, map.get(i) +1);

System.out.println(“The string contains duplicate charecter ” + i);

}

else

map.put(i, 1);

}

}

}

Advertisements

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