Big O Notation: Understanding the Efficiency of Algorithms - Constant Time

What is Constant Time?

Simply, constant time means that no matter how much input or the size of data you insert into the operation, the time of operation is still the same. If you still confusing about it. Let's see this chunk of code


val theArray = listOf<String>("glass", "spoon", "plate")

fun getTheItem(inputArray: Array<String>) : String{
  return inputArray[0]
}

In this case, the operation only takes one operation to get the first item from the array, which is the glass. We could have just three elements or one million elements but the operation will be constant because just take the first array because the requirement just to return the value of the first index in the array.

Conclusion

No matter the size of the input, in this case, the value of the array or the number of steps or operations just always needs one.