what is the time comlpexity of searching an element in an array?
is it o(1) or o(n) ?
2 Likes
For accessing the element in array the time complexity is o(1)
time complexity for an operation is basically how much time is required.
for searching an element, the best case is when the element is at first position, here we can find the element after searching for only one time. hence, O(1)
The worst case is when element is at end of array, ie. we have to search through entire array to reach last position. Hence, if we have n elements in an array, the time will be O(n)
1 Like