DistributedAI
DistributedAI

Reputation: 193

Can we reduce Time complexity O(n) of preparing a Java HashSet from an ArrayList?

There is an ArrayList of 10000+ items. I am trying to make them unique through a HashSet which is a operation of O(n) complexity. Is there any other algorithm / DS which can make a Collection unique with lower complexity than O(n)?

Upvotes: 2

Views: 498

Answers (2)

Aman gupta
Aman gupta

Reputation: 39

Without going through all the elements once, it is not possible to confirm that your set has all unique values. Hence, O(n) is minimum possible.

Upvotes: 1

Louis Wasserman
Louis Wasserman

Reputation: 198471

No, this is literally impossible. O(n) is the minimum complexity to so much as read through the ArrayList, let alone do anything with the elements.

Upvotes: 7

Related Questions