title: any better solution to get duplicates and there count in an array?
body: <p>I have the below code which is aimed at finding out a duplicate element in an array and printing it with its count.</p>
  
  <p>I have used the below approach to solve the problem.</p>
  
  <ol>
  <li>copy the original array into temp array</li>
  <li>find the duplicates in original array and remove the same</li>
  <li>copy the non duplicate array into another temp array</li>
  <li>compare non duplicate array with original array and print out the duplicates with their counts.</li>
  </ol>
  
  <p>For calculating the length I am using my own version of code.</p>
  
  <p>It o/ps 
  5 4
  2 3</p>
  
  <p>which is correct but I want to know if we can still refine this code.</p>
  
  <p>Please suggest best approach, condition is that there should not be any build in method/functions usage.</p>
  
  <pre><code>package codingpractice;
  
  public class DuplicateCount {
  public static void main(String argsp[]) throws Exception {
      int array[] = { 5, 1, 2, 5, 3, 2, 2, 5, 5 };
      DuplicateCount hw = new DuplicateCount();
      int length = hw.length(array);
      hw.duplicates(array, length);
  }
  
  public void duplicates(int array[], int length) throws Exception {
  
      int end =length, dupCount = 0;
  
      //copying to another array for later comparison
      int[] aarray = new int[length];
      for (int i = 0; i &lt; end; i++) {
          aarray[i] = array[i];
      }
  
      //finding duplicates and removing the same
      for (int i = 0; i &lt; end; i++) {
          for (int j = i + 1; j &lt; end; j++) {
              if (array[i] == array[j]) {
                  int shiftLeft = j;
                  for (int k = j + 1; k &lt; end; k++, shiftLeft++) {
                      array[shiftLeft] = array[k];
                  }
                  end--;
                  j--;
              }
          }
      }
  
      //copying non duplicates to another array
      int[] tarray = new int[end];
      for (int i = 0; i &lt; end; i++) {
          tarray[i] = array[i];
  
      }
  
      //Printing duplicates and there counts, comparing original array and non     duplicate array
      for (int i = 0; i &lt; length(tarray); i++) {
          dupCount = 0;
          for (int j = 0; j &lt; length; j++) {
              if (tarray[i] == aarray[j]) {
                  dupCount++;
              }
          }
          if (dupCount &gt; 1) {
              System.out.println(tarray[i] + " " + dupCount);
          }
      }
  }
  
  //length of array- not using inbuild function
  int length(int array[]) throws Exception {
      int count = 0;
      int temp = 0;
      try {
          while (true) {
              count++;
              temp = array[count];
          }
      } catch (Exception e) {
          return count;
      }
  }
  }
  </code></pre>