For a problem, I have to write a code that counts the number of singletons, stored in the variable n, in a string sequence. A singleton is a word in a sequence that does not appear before or after itself in the sequence.
I initialized the count variable n with 2, since the first and last tokens can only have one
copy before and after each other respectively. I divided the string sequence into tokens by splitting it with respect to the empty spaces between them.
Here is my code.
String string = " "; string = stdin.next(); String[] tokens = string.split(" "); n = 2; for (int i = 1; i < tokens.length()-1; i++){ while (!(tokens[i].equals("xxxxx"))){ if (!(tokens[i].equals(tokens[i-1]) && !(tokens[i].equals(tokens[i+1]))) n++; } }