title: Counting number of occurrences of Array element in a RDD
body: <p>I have a RDD1 with Key-Value pair of type <code>[(String, Array[String])]</code> (i will refer to them as (X, Y)), and a Array Z[String].
  I'm trying for every element in Z to count how many X instances there are that have Z in Y. I want my output as ((X, Z(i)), #ofinstances).</p>
  
  <pre><code>RDD1= ((A, (2, 3, 4), (B, (4, 4, 4)), (A, (4, 5)))
  Z = (1, 4)
  </code></pre>
  
  <p>then i want to get:</p>
  
  <pre><code>(((A, 4), 2), ((B, 4), 1))
  </code></pre>
  
  <p>Hope that made sense.
  As you can see over, i only want an element if there is atleast one occurence.</p>
  
  <p>I have tried this so far:</p>
  
  <pre><code>val newRDD = RDD1.map{case(x, y) =&gt; for(i &lt;- 0 to (z.size-1)){if(y.contains(z(i))) {((x, z(i)), 1)}}}
  </code></pre>
  
  <p>My output here is an RDD[Unit]</p>
  
  <p>Im not sure if what i'm asking for is even possible, or if i have to do it an other way. </p>