Reputation: 674
I'm wondering what the big O notation would be this javascript algorithm:
const a = [
{
b: [1, 2, 3],
},
{
b: [4, 5, 6],
},
{
b: [7, 8],
},
{
b: [9, 10, 11, 15, 61],
},
];
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a[i].b.length; j++) {
console.log(a[i].b[j]);
}
}
where the array a
contains an arbitrary number of objects, each containing an array b
of arbitrary length.
Intuitively I think the complexity of the algorithm woudln't grow exponentially, so the best guess I came up with is something like O(n) + O(m)
. I'm wondering if that is correct and if there's a better way to write this as Big O.
Upvotes: 0
Views: 236
Reputation: 1843
You pass throught each final element once and only once.
So the complexity is just O(n) with n the total number of elements.
Upvotes: 1