title: Count and remove similar elements in a list while iterating through it
body: <p>I used many references in the site to build up my program but I'm kind of stuck right now. I think using <code>iterator</code> will do the job. Sadly even though I went through questions which had iterator, I couldn't get the way of using it properly to implement it on my code.</p>
  
  <p>I want to,
        1. remove the similar elements found in the list <code>fname</code> 
        2. count &amp; add the that count of each element found in <code>fname</code> to 
            <code>counter</code>. </p>
  
  <p>Please help me do the above using iterator or with any other method. Following is my code,</p>
  
  <pre><code>List&lt;String&gt; fname = new ArrayList&lt;&gt;(Arrays.asList(fullname.split(""))); //Assigning the string to a list//
  
      int count = 1;
      ArrayList&lt;Integer&gt; counter = new ArrayList&lt;&gt;();
      List&lt;String&gt; holder = new ArrayList&lt;&gt;();
  
      for(int element=0; element&lt;=fname.size; element++)
      {
          for(int run=(element+1); run&lt;=fname.size; run++)
          {
              if((fname.get(element)).equals(fname.get(run)))
              {
                  count++;
                  holder.add(fname.get(run));
              }
  
              counter.add(count);                    
          }
  
          holder.add(fname.get(element));
          fname.removeAll(holder);
      }
  
      System.out.println(fname);
      System.out.println(counter);
  </code></pre>
  
  <p>Thanks.</p>