Can someone tell me the time complexity of the below code?
a
is an array of int.
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < a.length; i++) {
if (set.contains(arr[i])) {
System.out.println("Hello");
}
set.add(arr[i]);
}
I think that it is O(n), but I'm not sure since it is using Set
and this contains methods as well. It is also calling the add
method of set
.
Can anyone confirm and explain what the time complexity of the entire above code is? Also, how much space would it take?