title: Counting Common Elements in a String Array
body: <p>I am trying to make a program to count common elements occuring in all the Strings in a String[] array. I have the following:-</p>
  
  <blockquote>
    <p>A  <em>master</em> array and a <em>flag</em> array both of size 26</p>
  </blockquote>
  
  <p>Now for each string: I am marking frequency 1 for each <em>character</em> that appears in the string without incrementing in <em>flag</em> array.</p>
  
  <p>Now I am adding the values of flag array to corresponding values of master array</p>
  
  <p>my code looks like this</p>
  
  <pre><code>for(String str : arr)
  {
      for(char ch : str.toCharArray())
      {
          flag[ch - 97] = 1;
          master[ch - 97] =master[ch -97] + flag[ch - 97];
      }
  }
  </code></pre>
  
  <p>My plan is to finally count elements in the master array that have value equal to input string array's length. <em>This count will represent the count of characters that are common to all the strings</em></p>
  
  <p>But my code has a flaw.</p>
  
  <p>if a String has duplicate elements for example, 'ball' (with 2 ls). The corresponding value of the element in <em>master</em> array is getting incremented again.
  Which makes its value larger than what I wanted.</p>
  
  <p>So this is what I did.</p>
  
  <pre><code>for(String str : arr)
  {
      newstr = "";  //to keep track of each character in the string
  
      for(char ch : str.toCharArray())
      {
          int counter = 0;
          for(int i = 0; i &lt; newstr.length();i++)
          {
              char ch2 = newstr.charAt(i);
                  if (ch == ch2 )
                  {
                      counter = counter + 1; //if duplicate
                      break;
                  }
          }
  
      if(counter == 1)
      {
          break;
      }
      flag[ch - 97] = 1;
      master[ch - 97] =master[ch -97] + flag[ch - 97];
      newstr = newstr + ch;
  
     }
  }
  </code></pre>
  
  <p>Is this the right approach? or could this code be more optimized? </p>