-1

I am creating a program that removes duplicate values based on 10 user inputs. However instead of having distinct values such as [1,2,3,4,5,6] I have zeros in my output as such [0, 1, 0, 2, 0, 3, 0, 4, 5, 6]. Some kind assistance on this matter would be greatly appreciated!

import java.util.*;
public class SOB23_2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    Scanner sc= new Scanner(System.in);
    int[]Array= new int[10];
    for(int i=0;i<Array.length;i++)
    {
        System.out.println("Enter number"+(i+1)+":");
        Array[i]=sc.nextInt();
    }
        System.out.println("The distinct values of the array are"+Arrays.toString(eliminateDuplicates(Array)));
    }
    public static int[]eliminateDuplicates(int[]list)
    {
        int [] temp=new int[list.length];


        for(int i=0;i<list.length-1;i++)
        {
            if(list[i]!=list[i+1])
            {
               temp[i]=list[i];

            }


        }
        temp[list.length-1]=list[list.length-1];


        return temp;
    }
}
Jens
  • 67,715
  • 15
  • 98
  • 113
  • If you want to remove the value, you'll have to use ArrayList, or make a new array with a size = originalSize - numElementsYouRemoved – sleepToken Dec 04 '19 at 20:02
  • 2
    Does this answer your question? [How to efficiently remove duplicates from an array without using Set](https://stackoverflow.com/questions/17967114/how-to-efficiently-remove-duplicates-from-an-array-without-using-set) – ggorlen Dec 04 '19 at 20:02

3 Answers3

0

First, you're assuming the duplicates will be adjacent. This may be correct (and can be achieved by sorting first).

But then in your loop you only maintain one position variable, i.

Think of the problem as involving reading from one array and writing to another. You need a "read position" in the input and a "write position" in the output.

Also your output array will be longer than needed, because you create it to be the length of the input array. So you might want to make two passes through the input array, the first pass being necessary to discover how many duplicates can be removed, and thus find out how long the output array needs to be.

Then in the second pass you could copy values from the read position in the input and store them in the write position in the output, but only if they are not the same value as the most recently stored output value.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
0

If you were ask to do that manually you can count the zeros in your array, make a new array with length myArray.length-countZero and put every number that's not a zero in the new array, your array has zeros because java primitives (int in this case) can not be null.

public int countZero(int[] a){
     int count=0;
     for(int i=0;i<a.length;i++){
          if(a[i]==0)
              count++;
     }
     return count;
}

public int[] removeZero(int[] a){
    int[] myArray=new int[a.length-countZero(a)];
    int count=0;
    for (int i=0;i<a.length;i++){
         if(a[i]!=0)
              myArray[count]=a[i];
    }
    return myArray;
 }

But if don't need to do it manually you can just use ArrayList is much easier.

Salem
  • 1
  • 3
0

After you are finished you can then do this.

You can do this before or after the array is returned.

      int[] array = { 0, 1, 0, 2, 0, 3, 0, 4, 5, 6
      };

      array = Arrays.stream(array).filter(val -> val != 0).toArray();
      System.out.println(Arrays.toString(array));

Prints

[1, 2, 3, 4, 5, 6]


WJS
  • 36,363
  • 4
  • 24
  • 39