Skip to content

Instantly share code, notes, and snippets.

@lifeparticle
Last active December 17, 2015 05:49
Show Gist options
  • Save lifeparticle/5560679 to your computer and use it in GitHub Desktop.
Save lifeparticle/5560679 to your computer and use it in GitHub Desktop.
Count the unique character in a String
// Author: Mahbub
// http://mahbubzaman.wordpress.com/2012/06/21/count-the-unique-character-in-a-string/
/*
input
“aaaaaa”
“aaa aaa”
“abcdeabcde”
“YESyes”
output
1
2
5
6
*/
public static int frq [] = new int [500];
public static int countUniqueChar (String line) {
Arrays.fill(frq, 0);
int ans = 0;
for(int i = 0; i < line.length(); ++i) {
if(frq[line.charAt(i)] == 0) {
frq[line.charAt(i)]++;
++ans;
}
}
return ans;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment