-3

I have 2 list of numbers and i only want the unique number to be the output which means the number that only appear once in the list. And it has to be double not integers.

Example:

list 1: {1.1, 8.0, 4.0, 1.1}

list 2: {1.1, 8.0, 4.0}

"8.0" and "4.0" are unique in both list 1 and list 2.

I need a way to identify numbers like 8.0 and 4.0 where it matches and occurs only once in each list.

I tried using this code but the output is not what i want.

Double[] i = {100.00, 100.00, 17246.40, 2568.00, 0.20, 9845.00, 5768.18, 0.20, 30.00, 63.68, 83.56, 444.39, 144.20, 
                2889.00};

        Set<Double> uniqKeys = new TreeSet<Double>();
        uniqKeys.addAll(Arrays.asList(i));

        System.out.println("List 1: " + uniqKeys);

This is the output for the above code

List 1: [0.2, 30.0, 63.68, 83.56, 100.0, 144.2, 444.39, 2568.0, 2889.0, 5768.18, 9845.0, 17246.4]

But what i want the output to be is

17246.4, 2568.0, 9845.0, 5768.18, 30.0, 63.68, 83.56, 444.39, 144.20, 2889.0

Thanks.

mary
  • 71
  • 1
  • 1
  • 6

1 Answers1

0

Try this:

Integer[] i = {1, 1, 2, 1, 3, 4, 5};
Set<Integer> st= new TreeSet<Integer>();
st.addAll(Arrays.asList(i));

or usinn Java8

Integer[] myarr = new Integer[]{1, 1, 2, 1, 3, 4, 5};
Stream.of(myarr).distinct().forEach(x -> System.out.print(" " + x));
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • You alternatively can pass in the `Arrays.asList(i)` as a constructor parameter, rather than making a separate call to `addAll`. – Andy Turner Apr 11 '16 at 09:51