Data Structures & AlgorithmsยทPractice drills 5

Drill 11: Rewrite it โ€” Complexity and Big O

Rewrite the example in your own words or structure.

Intermediate 5 min read Lesson 116 of 300 Challenge included
What you will be able to do
  • Rewrite the example in your own words or structure.

Practice task

Rewrite the example in your own words or structure.

complexity.jsJavaScript
// O(n): one pass
function total(values) {
  let sum = 0
  for (const value of values) sum += value
  return sum
}

// O(n^2): compare every pair
function hasDuplicate(values) {
  for (let i = 0; i < values.length; i++)
    for (let j = i + 1; j < values.length; j++)
      if (values[i] === values[j]) return true
  return false
}
Your turn

Write your answer

Easy

Rewrite the example in your own words or structure.

TEXT ยท starter
Your answerโ€ฆ
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 116 of 300 ยท 0% of course