Kootling Kootling - 3 months ago 10
Java Question

Removing duplicates from an array (without sets or sorting)

I have the following 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 < 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 < arr.length; i++) {
boolean b = true; //boolean that checks if an element is a duplicate
for (int j = i+1; j < 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 < unique.length; i++) {
System.out.println(unique[i]);
}
}
}


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
unique
array will be set to
0
, the duplicate elements from the first array are being set to
0
in
unique[]
(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
Integer.MIN_VALUE
in the unique array and then printing only the elements of
unique[]
which are not equal to
Integer.MIN_VALUE
, but that seems like a weak solution to the problem. How can I fix this problem?

EDIT: If I run the code:


How many numbers are you going to enter? 4

1

2

2

0


Output:

1
0
2
0


Since the second element of the array is a duplicate, I do not set
unique[1]
to any value, making it default to 0. How do I avoid printing that 0, since it is not part of the original array?

EDIT 2: 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.

Answer

I am going to use the tools that you used to solve the problem, cuz something in me is telling me this is homework...

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 < arr.length; i++)// enter numbers into array
      {

         arr[i] = scan.nextInt();

      }

      double[] unique = new double[arr.length];    //initialize new array that will hold unique values

      ///////My edit

      for(int z = 0; z < unique.length; z++)
      {

         unique[z] = -0.5;

      }

      ///////

      for (int i = 0; i < arr.length; i++) 
      {

         boolean b = true;    //boolean that checks if an element is a duplicate

         for (int j = i+1; j < 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 < unique.length; i++) 
      {

         if(!(unique[i] == -0.5))
         {

            System.out.println((int)(unique[i]));

         }

      }

   }
}

So, you see where I commented out my edit? that's the new thing, a very easy way to check is to give the values a number that is not expected, in this case, a negative number. Now, that is an assumption on my part, change -1 to whatever value you know will not be entered into that Scanner. Same for the if statement.