2

Have a program where the user inputs 10 int values into the array. Lastly I need to pull out the distinct values and display them. Added my second for loop which would determine if the the value is distinct (i.e. meaning if the number appears multiple times it is only displayed once).

For instance, let say I pass in the numbers: 1, 2, 3, 2, 1, 6, 3, 4, 5, 2 the distinct array should only contain numbers {1, 2, 3, 6, 4, 5}

import java.util.Scanner;
import java.io.*;

public class ArrayDistinct {
 public static void main(String[] args) throws IOException {

 Scanner input = new Scanner(System.in);

 // Create arrays & variables  
 int arrayLength = 10;
 int[] numbers = new int[arrayLength];
 int[] distinctArray = new int[arrayLength];
 int count = 0;

 System.out.println("Program starting...");
 System.out.print("Please enter in " + numbers.length + " numbers: ");

 for (int i = 0; i < numbers.length; i++) {
  numbers[i] = input.nextInt();
 }

 for (int i = 0; i < numbers.length; i++) {
  int temp = numbers[i];
  int tempTwo = numbers[i + 1];

  if (tempTwo == temp) {
   count++;
   distinctArray[i] = temp;
  }
 } 

 // Print out results

} // end main
} // end class
Chewy
  • 85
  • 2
  • 4
  • 15

7 Answers7

5

In Java 8

Stream< T > distinct()

Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.

Code:

   Integer[] array = new Integer[]{5, 10, 20, 58, 10};
   Stream.of(array)
         .distinct()
         .forEach(i -> System.out.print(" " + i));

Output:

5,10,20,58

Read More About distinct function

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • @Chewy take a look at this link for more info http://stackoverflow.com/questions/17967114/how-to-remove-duplicates-from-an-array-in-java – Kick Buttowski Oct 31 '14 at 17:32
4

Try this :

Set<Integer> uniqueNumbers = new HashSet<Integer>(Arrays.asList(numbers));

uniqueNumbers will contain only unique values

ToYonos
  • 16,469
  • 2
  • 54
  • 70
2

Try this code.. it will work

package Exercises;
import java.util.Scanner;
public class E5Second
{
    public static void main(String[] args) 
    {
        Scanner In = new Scanner(System.in);
        int [] number = new int [10];
        fillArr(number);

        boolean [] distinct = new boolean [10];

        int count = 0; 
        for (int i = 0; i < number.length; i++) 
        {
            if (isThere(number,i) == false)
            {
                distinct[i] = true;
                count++;
            }
        }
        System.out.println("\nThe number of distinct numbers is  " + count);
        System.out.print("The distinct numbers are: ");
        displayDistinct(number, distinct);
    }
    public static void fillArr(int [] number)
    {
        Scanner In = new Scanner(System.in);
        System.out.print("Enter ten integers ");
        for (int i = 0; i < number.length; i++)
            number[i] = In.nextInt();
    }
    public static boolean isThere(int [] number, int i)
    {
        for (int j = 0; j < i; j++)
            if(number[i] == number[j])
                return true;
        return false;
    }
    public static void  displayDistinct(int [] number, boolean [] distinct)
    {
        for (int i = 0; i < distinct.length; i++)
            if (distinct[i]) 
                System.out.print(number[i] + " ");
    }
}
Display Name
  • 4,502
  • 2
  • 47
  • 63
Anas K
  • 61
  • 1
  • 3
1

One possible logic: If you're supposed to only sort out "unique" numbers, then you'll want to test each number as it's entered and added to the first array, and loop through the array and see if it's equal to any of the numbers already there; if not, add it to the "unique" array.

geonz
  • 184
  • 1
  • 2
  • 12
0

Sets in java doesn't allow duplicates:

    Integer[] array = new Integer[]{5, 10, 20, 58, 10};
    HashSet<Integer> uniques = new HashSet<>(Arrays.asList(array));

That's it.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
0

Something like this should work for you:

     Scanner input = new Scanner(System.in);

     // Create arrays & variables  
     int arrayLength = 10;
     int[] numbers = new int[arrayLength];
     int[] distinctArray = new int[arrayLength];
     int count = 0;
     Set<Integer> set = new HashSet<Integer>();

     System.out.println("Program starting...");
     System.out.print("Please enter in " + numbers.length + " numbers: ");

     for (int i = 0; i < numbers.length; i++) {
         set.add(input.nextInt());
     }

     for(Integer i : set)
     {
         System.out.println("" + i);
     }

This will only add unique values to the set.

brso05
  • 13,142
  • 2
  • 21
  • 40
0
int a[] = { 2, 4, 5, 3, 3, 3, 4, 6 };
int flag = 0;
for (int i = 0; i < a.length; i++)
{  
    flag = 0;
    for (int j = i + 1; j < a.length; j++)
    {
        if (a[i] == a[j])
        {
            flag = 1;
        }
    }

    if (flag == 0)
    {
        System.out.println(a[i]);
    }
}
ManoDestra
  • 6,325
  • 6
  • 26
  • 50