I have a already sorted string array named arr and Supposing I enter the sentence
hello how hello to how me in
Desired Output is
hello 2
how 2
me 1
in 1
to 1
Here's how I am trying to count it
int counter = 1;
for(j1 = 0; j1 < arr.length; j1++){
if(j1 + 1 < arr.length){
if(arr[j1].equals(arr[j1 + 1])){
counter++;
} else {
System.out.println(arr[j1] + " " + counter);
counter = 1;
}
}
}
but this is not working , please help?