When you have a function that has multiple non-nested loops, does that change the time complexity from O(N)? I know that if you were to write a function that logs every element of an array, you could do this with a single loop, giving you a bigO of O(N), but if you added more loops, would that give you a bigO of O(N * number of loops)?
Consider this string compression function, what is it's bigO given that it loops over the string multiple times:
function compression(string) {
let compressed = "";
let map = {
}
string.split("").forEach((e, i) => {
map[e] = 0;
})
string.split("").forEach((e, i) => {
map[e] += 1;
})
for (var element in map) {
compressed += element + map[element];
}
if (compressed.length > string.length) {
return string;
} else {
return compressed;
}
}