How to remove duplicates from a list using an auxiliary array in Java?

前端 未结 8 782
南方客
南方客 2020-12-10 19:44

I am trying to remove duplicates from a list by creating a temporary array that stores the indices of where the duplicates are, and then copies off the original array into a

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 20:25

    Instead of doing it in array, you can simply use java.util.Set.

    Here an example:

    public static void main(String[] args)
    {
        Double[] values = new Double[]{ 1.0, 2.0, 2.0, 2.0, 3.0, 10.0, 10.0 };
        Set singleValues = new HashSet();
    
        for (Double value : values)
        {
            singleValues.add(value);
        }
        System.out.println("singleValues: "+singleValues);
        // now convert it into double array
        Double[] dValues = singleValues.toArray(new Double[]{});
    }
    

提交回复
热议问题