1

I'm trying to write a function that returns an array of unique numbers. The array must be sorted. The function works fine when there are more than just one element, but it doesn't work for when there is just one element in the array. The function should return the only element found in that array, but instead, it is returning an empty array.

Why is this happening?

Array must be sorted for numUnique and removeDuplicates

    public static int numUnique (double[] list){
    int uniques = 1;
    int i = 1;
    if(list.length == 0) return 0;
    while(i<list.length){
        if(list[i] != list[i - 1])
            uniques++;
        i++;
    }
    return uniques;
}

    public static double[] removeDuplicates(double[] list){
    double[] arrayOfUniques = new double[numUnique(list)];
    if(list.length == 0) return arrayOfUniques; 

    int uniques = 1;
    int i = 1;
    arrayOfUniques[0] = list[0];
    while(i < list.length){
        if(list[i] != list[i - 1])
            arrayOfUniques[uniques++] = list[i];
        i++;
    }
    return arrayOfUniques;
}

Array:

double[] a = {11,11,21,31,41,41,51,61,61,61,71,71,81,91,91,100,100};

Output:

Unique numbers: 9

Array of uniques: [11.0, 21.0, 31.0, 41.0, 51.0, 61.0, 71.0, 81.0, 91.0]

But it doesn't work when the array just has one element:

 double[] a = {11};

Output:

Unique numbers: 0

Array of uniques:[]

J.user94
  • 81
  • 11
  • 2
    Have you tried to put some breakpoint and see what happened when you pass array with one element? – Liu Mar 24 '17 at 15:46
  • 1
    What would you expect `while(i < list.length-1)` to do when `i == 0` (as it is the first time through that loop) and `list.length == 1`? – yshavit Mar 24 '17 at 15:51
  • Why don't you use a Set or a List? http://stackoverflow.com/questions/3064423/how-to-convert-an-array-to-a-set-in-java – briosheje Mar 24 '17 at 15:51
  • Thank you. I saw the error. – J.user94 Mar 24 '17 at 16:00
  • Please think about accepting an answer or vote up, this is how a forum works, newcomers will more easily go to see accepted post rather than non-accepted – azro Aug 09 '18 at 08:14

4 Answers4

1

Try this.. Convert your array to list & convert list to set

Set not have duplicate values.. simple

List<String> myList= Arrays.asList(a);//convert array to List
Set<Double> uniqueSet = new HashSet<Double>(myList);//you get unique values

If you want to Set to Array try this https://stackoverflow.com/a/5982478/3879847

Community
  • 1
  • 1
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
0
public List<Double> removeDuplicates (List<Double> list){
        // add elements to al, including duplicates
        Set<Double> hs = new HashSet<Double>();
        hs.addAll(list);
        list.clear();
        list.addAll(hs);
        return list;
    }
0

Actually, it doesn't work for arrrays having only one unique element either (e.g. it returns empty array for new double[]{11.0,11.0, 11.0}). I would recommed a simpler approach using Java 8's stream:

public static double[] removeDuplicates(double[] list){
    LinkedHashSet<Double> uniqueElements = Arrays.stream(list)
    .boxed()
    .collect(Collectors.toCollection(LinkedHashSet::new));
    return uniqueElements.stream().mapToDouble(d -> d).toArray();
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

With Java 8 we get Stream, so how to use it in this case :

public static void main (String[] args) {
    double[] a = {11,11,21,31,41,41,51,61,61,61,71,71,81,91,91,100,100};
    //Way 1 just to print the unique element
    DoubleStream.of(removeDuplicate(a)).forEach(System.out::println);
    //Way 2 to save the update the array with only the unique element
    a = removeDuplicate(a);
}

private static double[] removeDuplicate(double[] a) {
    return DoubleStream.of(a).distinct().sorted().toArray();
} 

In the main, we're just calling the method, and then read the result and print the values

In the removeDuplicate method : we create Stream of the values, then we keep only the different elements, then we sort them, and them we come back to an array (to follow your wishes)

If you're not sure to HAVE to use array, better use List every time ;)

azro
  • 53,056
  • 7
  • 34
  • 70