@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.
- Storage in Set -> O(n)
- Storage in array generated due to set.toArray - O(n)
- Storage in the newly created list - O(n)
Total O(3n) which is same as O(n).
Can you confirm this for me?