Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
O(n) or Linear Complexity Gist
const array = [50, 24, 13, 64, 85]
function add_numbers(array) {
let total = 0 //O(1) --> constant complexity (a variable is initialized)
for (let i = 0; i < array.length; i++) { //O(n) --> linear complexity (we loop through an arra, memory space is allocated to each element of the array)
total += array[i] //O(1) --> constant complexity (a variable is re-assigned)
}
return total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment