Unique values of an array I'm curious if there is another way to code this. Could it be done with a while loop for instance?

public static int numUnique(double[] list) {
    int uniques = 0;
    if(list.length == 0){return 0;}
    for(int i = 0; i &lt; list.length; i++){
        int dups = 0;
        for(int j = i + 1; j &lt; list.length; j++){
            if(list[i] == list[j]){
                dups = dups + 1;
            }
        }
        if(dups == 0){
            uniques = uniques + 1;
        }
    }
    return uniques;
}
No need for any loops with Java 8 streams.


Get a DoubleStream from the array with Arrays.stream(double[] array) so that you can
get the distinct() elements from that
which you can count() to retrieve your result.




public static long numUnique(double[] list) 
{
    return Arrays.stream(list).distinct().count();
}


As count() returns a long, I modified the signature of your method to return long, too. It's within the responsibility of the user of your method then to perform any cast on the result.

Full example program:

import java.util.Arrays;

public class UniqueDoubles
{
    public static void main(String[] args)
    {
        double[] doubles = {1, 2, 3.5, 3.5, 2, 345345.345345};

        System.out.println(numUnique(doubles));
    }

    public static long numUnique(double[] list) 
    {
        return Arrays.stream(list).distinct().count();
    }
}