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

Find the first unrepeated character in a string.

July 7, 2012

public static 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;

}
Advertisements
Categories: Algorithms
%d bloggers like this: