Skip to content

Instantly share code, notes, and snippets.

@tcelovsky
Created January 6, 2022 23:05
Show Gist options
  • Save tcelovsky/af276675d6c562f4e7e8febe4d7a43e6 to your computer and use it in GitHub Desktop.
Save tcelovsky/af276675d6c562f4e7e8febe4d7a43e6 to your computer and use it in GitHub Desktop.
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