title: Removing duplicates from an array (without sets or sorting)
body: <p>I have the following code:</p>
  
  <pre><code>import java.util.Scanner;
  public class ArrayDuplicates {
      public static void main(String[] args) {
          Scanner scan = new Scanner(System.in);
          System.out.print("How many numbers are you going to enter? ");
          int num = scan.nextInt();
          int[] arr = new int[num]; // initialize array with user inputted length
          for (int i = 0; i &lt; arr.length; i++) { // enter numbers into array
              arr[i] = scan.nextInt();
          }
  
          int[] unique = new int[arr.length];    //initialize new array that will hold unique values
          for (int i = 0; i &lt; arr.length; i++) {
              boolean b = true;    //boolean that checks if an element is a duplicate
              for (int j = i+1; j &lt; arr.length; j++) {    //check all elements above int i
                  if (arr[i] == arr[j]) {
                      b = false;    // set b to false if there is an existing duplicate
                  }
              }
              if (b) {
                  unique[i] = arr[i];    // if no duplicates exist, then it is unique.
              }
          }   
          for (int i = 0; i &lt; unique.length; i++) {
                  System.out.println(unique[i]);
          }
      }
  }
  </code></pre>
  
  <p>The problem with this code (aside from being horribly slow for large arrays, but that's not the point) is that since the undeclared elements for the <code>unique</code> array will be set to <code>0</code>, the duplicate elements from the first array are being set to <code>0</code> in <code>unique[]</code> (if that makes any sense). I understand why this happens, but cannot find an efficient way to fix this. I tried setting the duplicate elements to <code>Integer.MIN_VALUE</code> in the unique array and then printing only the elements of <code>unique[]</code> which are not equal to <code>Integer.MIN_VALUE</code>, but that seems like a weak solution to the problem. How can I fix this problem?</p>
  
  <p><strong>EDIT:</strong> If I run the code:</p>
  
  <blockquote>
    <p>How many numbers are you going to enter? 4<br>
    1<br>
    2<br>
    2<br>
    0</p>
  </blockquote>
  
  <p>Output:</p>
  
  <pre><code>1
  0
  2
  0
  </code></pre>
  
  <p>Since the second element of the array is a duplicate, I do not set <code>unique[1]</code> to any value, making it default to 0. How do I avoid printing that 0, since it is not part of the original array?</p>
  
  <p><strong>EDIT 2:</strong> Yes, this is homework, but the reason I don't want to use sets, sorting, etc. is primarily that I am not familiar with them. Also, as I am not asking anyone to write the entire program for me, I think it's fine to ask for a little help.</p>