This is the reference code:
// Create an array of size 256 i.e. ASCII_SIZE
int count[] = new int[MAX_CHAR];
int len = str.length();
// Initialize count array index
for (int i = 0; i < len; i++)
count[str.charAt(i)]++;
// Create an array of given String size
char ch[] = new char[str.length()];
for (int i = 0; i < len; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
// If any matches found
if (str.charAt(i) == ch[j])
find++;
}
if (find == 1)
System.out.println("Number of Occurrence of " +
str.charAt(i) + " is:" + count[str.charAt(i)]);
}
The output is supposed to resemble:
Number of occurrence of 'x' is: 'times it occured'
If the letter has occurred previously then only display the occurrence once.
I get the logic using 2 for loops, although my teacher said its possible to execute this application using only 1 for loop.
the issue i'm running into is:
I'm able to find if the character has been already found only if they are beside each other.
How are you expected to check if all the previous character have been found without another for loop?