Pulling distinct values from a array in java

核能气质少年 提交于 2019-12-11 06:17:03

问题


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

回答1:


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




回答2:


Try this :

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

uniqueNumbers will contain only unique values




回答3:


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] + " ");
    }
}



回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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]);
    }
}


来源:https://stackoverflow.com/questions/26679860/pulling-distinct-values-from-a-array-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!