title: How to only return ArrayList of strings with minimum number of occurrences?
body: <p>I have a <code>String[]</code>, <code>originalStringArray</code>, that has duplicates in them. So <code>{"dog","cat","dog","fish","dog","cat"}</code>. </p>
  
  <p>I wanted to make a function that only returns the strings that occur exactly a certain number of times. For here, if I said 3, it would return "dog" but not "cat". </p>
  
  <p>Here's my current code: </p>
  
  <pre><code>public ArrayList&lt;String&gt;  returnMultiples(String[] originalStringArray,int requiredCount){
      ArrayList&lt;Integer&gt; mCount = new ArrayList&lt;&gt;();
      List&lt;String&gt; list = Arrays.asList(originalStringArray);
      ArrayList&lt;String&gt; result = new ArrayList&lt;&gt;();
  
      // Count occurrences in original string
      for(String item: originalStringArray){
          mCount.add(Collections.frequency(list,item));
      }
  
      // If frequency is equal to count, add to array list
      for(int i=0; i&lt;mCount.size(); i++){
          if(mCount.get(i) == requiredCount){
              result.add(originalStringArray[i]);
          }
      }
  
      return result;
  }
  </code></pre>
  
  <p>The problem I have is, I read somewhere that the Collections library is very slow and drag, and it also seems like that this method could be reduced using HashSets and tables. Unfortunately, I'm kind of at a loss on how to do that. Is there a better way to do this?</p>