title: Getting the Elements that has duplicates in an int array Java
body: <p>This post - "<a href="https://stackoverflow.com/questions/31738717/java-count-duplicates-from-int-array-without-using-any-collection-or-another-i">Java + Count duplicates from int array without using any Collection or another intermediate Array</a>", was also a sample exercise in my book at school, but what I want to do is get the elements that has duplicates without sorting it.</p>
  
  <p>What I did is I removed the duplicates of arrays first, to get only the unique elements and then I compare it to the original array and count how many times the element has been found. But the problem is it doesn't print the correct elements which has duplicates.</p>
  
  <pre><code>int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1};
  </code></pre>
  
  <p>the correct output should be: <code>7, 1, 4</code></p>
  
  <p>but instead it outputs: <code>7, 6, 1</code></p>
  
  <p>This is my codes:</p>
  
  <pre><code>//method for removing duplicates
  public static int[] removeDuplicates(int[] n) {
      int limit = n.length;
  
      for(int i = 0; i &lt; limit; i++) {
          for(int j = i + 1; j &lt; limit; j++) {
              if(n[i] == n[j]) {
                  for(int k = j; k &lt; limit - 1; k++) {
                      n[k] = n[k + 1];
                  }
                  limit--;
                  j--;
              }
          }
      }
  
      int[] uniqueValues = new int[limit];
      for(int i = 0; i &lt; uniqueValues.length; i++) {
          uniqueValues[i] = n[i];
      }
  
      return uniqueValues;
  }
  
  //method for getting elements that has duplicates
  public static int[] getDuplicatedElements(int[] n) {
      int[] nCopy = n.clone();
      int[] u = removeDuplicates(nCopy);
      int count = 0;
      int limit = u.length;
  
      for(int i = 0; i &lt; u.length; i++) {
          for(int j = 0; j &lt; n.length; j++) {
              if(u[i] == n[j]) {
                  count++;
              }
          }
  
          if(count == 1) {
              for(int k = i; k &lt; limit - 1; k++) {
                  u[k] = u[k + 1];
              }
              limit--;
          }
          count = 0;
      }
  
      int[] duplicated =  new int[limit];
      for(int i = 0; i &lt; duplicated.length; i++) {
          duplicated[i] = u[i];
      }
  
      return duplicated;
  }
  
  //main
  public static void main(String[] args) {
      int[] num = {7, 2, 6, 1, 4, 7, 4, 5, 4, 7, 7, 3, 1};
  
      //printing original values
      System.out.print(Arrays.toString(num));
      System.out.println();
  
  
      int[] a = getDuplicatedElements(num);
      System.out.print("Elements with Duplicates: " + Arrays.toString(a)); 
  }
  </code></pre>
  
  <p>What's the error in my codes here? Please help thanks...</p>