title: Find common elements in two unsorted array
body: <p>I try to find a solution to this problem:
  I have two arrays A and B of integers (A and B can have different dimensions). I have to find the common elements in these two arrays. I have another condition: the maximum distance between the common elements is k.
  So, this is my solution. I think is correct:</p>
  
  <pre><code>for (int i = 0; i&lt;A.length; i++){
      for (int j=jlimit; (j&lt;B.length) &amp;&amp; (j &lt;= ks); j++){
          if(A[i]==B[j]){
              System.out.println(B[j]);
              jlimit = j;
              ks = j+k;
          }//end if
      }
  }
  </code></pre>
  
  <p>Is there a way to make a better solution? Any suggestions? Thanks in advance!</p>