Home > Algorithms > Find the first unrepeated character in a string

Find the first unrepeated character in a string

December 16, 2011

public class FirstUnRepeatedChar
{
public char findFirstUnrepeatedCharacter(String string)
{
int repeatCount[] = new int[256];
for(int i=0;i
char c = string.charAt(i);
repeatCount[c]+=1;
}
for(int i=0;i<string.length();i++){
char c = string.charAt(i);
if(repeatCount[c]==1)
return c;
}
return 0;
}

public static void main(String[] args)
{
System.out.println(new FirstUnRepeatedChar().findFirstUnrepeatedCharacter("vidya"));
}
}
Advertisements
Categories: Algorithms
%d bloggers like this: