-1
@SuppressWarnings("unchecked")
public static <T> List<T> eliminateDuplicate(List<T> list) {
    Set<T> set = new HashSet<T>(list);
    return (List<T>) Arrays.asList(set.toArray());
}

Wanted check the space complexity of the simple code above to eliminate dulplicates.

  1. Storage in Set -> O(n)
  2. Storage in array generated due to set.toArray - O(n)
  3. Storage in the newly created list - O(n)

Total O(3n) which is same as O(n).

Can you confirm this for me?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132

1 Answers1

0

Basically, yes. The total space utilization is O(N) where the final (de-duplicated) list size is N.

But Arrays.asList(...) is creating a wrapper that occupies O(1) space (in addition to the array that it is wrapping).

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216