find the duplicate word from a sentence with count using for loop

大兔子大兔子 提交于 2019-12-05 18:21:02

You need to print the result only for the outer loop. Also, you need to avoid checking the words that were already checked in previous iteration:

for (int i = 0; i < word.length; i++) {
    int count = 0; // reset the counter for each word

    for (int j = 0; j < word.length; j++) {

        if (word[i].equals(word[j])) {
            /* if the words are the same, but j < i, it was already calculated
               and printed earlier, so we can stop checking the current word
               and move on to another one */
            if (j < i) {
                break; // exit the inner loop, continue with the outer one
            }

            count++;
        }
    }

    if (count > 1) {
        System.out.println("the word " + word[i] + " occured " + count + " time");
    }
}

UPDATE

Additional explanation around this code: if (j < i) { break; }

i is the index of the word we calculate duplicates for, j is the word we compare it against. Since we start always from beginning, we know that if the words are equal while j < i, it was already processed in earlier run of the outer loop.

In this case, using break, we interrupt the inner loop and the flow continues in the outer loop. As we didn't update count at all, it is still zero and thus the condition for printing the result if (count > 1) is not satisfied and the println is not executed.

Example for the word "hello", using simple pseudo-code in the following part.

For its first occurrence:

count = 0
    i = 1, j = 0 --> hello != hi                  --> do nothing
    i = 1, j = 1 --> hello == hello, j is not < i --> count++
    i = 1, j = 2 --> hello != hi                  --> do nothing
    i = 1, j = 3 --> hello != good                --> do nothing
    i = 1, j = 4 --> hello != morning             --> do nothing
    i = 1, j = 5 --> hello == hello, j is not < i --> count++
count > 1        --> print the result

For its second occurrence:

count = 0
    i = 5, j = 0 --> hello != hi           --> do nothing
    i = 5, j = 1 --> hello == hello, j < i --> break, we have seen this pair earlier
count is not > 1 --> result not printed

Hope I didn't make things more complicated with this example

Print Outside first for loop,This will do the trick and initialize count=0 in for loop beginning

 for( int i=0;i<word.length;i++)
             {
                count=0;
                for( int j=0;j<word.length;j++)
                   {
                       if(word[i].equals(word[j]))
                          {
                             count++;
                          }
                   }
     if(count>1)
     System.out.println("the word "+word[i]+" occured"+ count+" time");
             }
enter code here
import java.util.*; 
class test{
public static void main(String[] args){
String s;
int count=0;
int count1=0;

System.out.println("Enter the Sentence");
Scanner scan=new Scanner(System.in);
s=scan.nextLine(); 
System.out.println(s);
String[] arr=s.split(" ");

String[] srr=new String[arr.length];
int[] rev=new int[arr.length];
 for(int i=0;i<arr.length; i++)
   { if(arr[i]!="NULL"){
   String temp=arr[i];

 for(int j=i+1;j<arr.length; j++)
 {
 if(temp.equals(arr[j]))
 {
   arr[j]="NULL";
 count++;

 }

   }

  srr[count1]=temp;
 rev[count1]=count;
 count=0;
 count1++;
  }

  }
 for(int i=0;i<count1;i++)
  System.out.println(srr[i]+"  "+rev[i]);
  }
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!