Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ArrayList indexOf complexity N?

I have N numbers in arraylist. To get the indexOf, arraylist will have to iterate maximum N times, so complexity is O(N), is that correct?

like image 680
good_evening Avatar asked Jan 27 '14 18:01

good_evening


People also ask

What is the complexity of ArrayList?

When we search any value in ArrayList or LinkedList, we have to iterate through all elements. This operation has O(N) time complexity.

What is the time complexity of indexOf?

indexOf() – also runs in linear time. It iterates through the internal array and checks each element one by one, so the time complexity for this operation always requires O(n) time.

Can you use indexOf for an ArrayList?

The index of a particular element in an ArrayList can be obtained by using the method java. util. ArrayList. indexOf().


1 Answers

Source Java API

Yes,Complexity is O(N).

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

like image 199
Nambi Avatar answered Oct 05 '22 01:10

Nambi