the assumption is the String is ASCII other wise check for the hash table function
For example If the input is "TEETOTALAR" The First non repeated Character is "O"
public static char FirstNonRepeatedArray(string stringToCheck)
{
int length = stringToCheck.Length;
int i = 0;
int[] intCollection = new int[256];
char returnChar = '\0';
for (i = 0; i < length; i++)
{
intCollection[stringToCheck[i]] = intCollection[stringToCheck[i]] + 1;
}
for (i = 0; i < length; i++)
{
if (intCollection[stringToCheck[i]] == 1)
{
returnChar = stringToCheck[i];
break;
}
}
return returnChar;
}
No comments:
Post a Comment