const arr = ['a','b','c'];
for (let char of arr) {
console.log(char);
}
In my analysis, the time complexity of the code above is O(n).
const arr = ['a','b','c'];
for (let char of arr) {
console.log(arr.indexOf(char);
}
However, I have concerns about whether indexOf() searches through all the elements. If it does, then I suspect that the time complexity of the code above could potentially be O(n^2).
I am curious to know if indexOf() behaves similarly to a for loop in terms of searching all the elements.