title: Pass two arrays of strings and count the occur y array in x array in Java
body: <p>I created a Java method which when passed two arrays of strings x and y, counts the number of times each string occurring in y occurs in x and prints the results in the order the strings appear in y. For example, look at the main function, it should output as ab: 2, dc: 1, ef: 0. My code didn't work as it outputs ab: 1, ab: 2, dc: 3.</p>
  
  <pre><code>public class stringOccurInArray {
      public static void stringOccurInY(String[] x, String[] y) {
          int count = 0;
          for(int i=0; i&lt;x.length; i++)  {
              for(int j=0; j&lt;y.length; j++) {
                  if(y[j].contains(x[i])) {
                      count++;
                      System.out.println(y[j] + ": " + count);
                  }
              }
          }
          count = 0; // reset the count
      }
  
      public static void main(String[] args) {
          String[] a = {"ab", "cd", "ab", "dc", "cd"};
          String[] b = {"ab", "dc", "ef"};
  
          stringOccurInY(a, b);
      }
  }
  </code></pre>