Rakim
Rakim

Reputation: 1107

What is the running time of .contains()

If set is a HashSet of integers..

HashSet<Integer> set = new HashSet<Integer>

What is the running time of the method .contains()

Example:

set.contains(int) 

I am using Java 7.

Upvotes: 3

Views: 4878

Answers (2)

whistler
whistler

Reputation: 886

The method contains of the HashSet class in Java operates in constant time. I would also add that other basic operations, such as add, remove, and size also operate in constant time.

Upvotes: 5

notbad
notbad

Reputation: 2887

To find a value in Hashset always takes const time O(1).

Upvotes: 1

Related Questions