/o(n²)_2.js Secret
Created
July 9, 2021 20:29
Star
You must be signed in to star a gist
O(n²) Example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const numbers = [1, 2, 3, 4, 5] | |
| function logAndSum(numbers) { | |
| for (let i = 0; i < numbers.length; i++) { //O(n) --> linear time (run time increases at the same pace as the input) | |
| for (let j = 0; j < numbers.length; j++) { //O(n) --> linear time (run time increases at the same pace as the input) | |
| console.log(numbers[i]); //O(1) --> constant time (basic operation) | |
| } | |
| } | |
| const sum = numbers.reduce((acc, number) => { //O(n) --> linear time (run time increases at the same pace as the input) | |
| return acc += number; //O(1) --> constant time (basic operation) | |
| }, 0); | |
| return sum; //O(1) --> constant time (basic operation) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment