Comparison operators in Swift - Time & Space Complexity
When we use comparison operators, we want to know how long it takes to compare values as the input grows.
We ask: How does the time to compare change when we have more items?
Analyze the time complexity of the following code snippet.
let numbers = [3, 7, 2, 9, 5]
let target = 7
var found = false
for number in numbers {
if number == target {
found = true
break
}
}
This code checks if a target number is in the list by comparing each item until it finds a match.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing each number to the target using
==. - How many times: Up to once for each item in the list, until a match is found.
As the list gets bigger, the number of comparisons can grow up to the size of the list.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 comparisons |
| 100 | Up to 100 comparisons |
| 1000 | Up to 1000 comparisons |
Pattern observation: The number of comparisons grows roughly in a straight line with the list size.
Time Complexity: O(n)
This means the time to find the target grows directly with the number of items you check.
[X] Wrong: "Comparing two values takes longer as the list grows."
[OK] Correct: Each comparison between two values takes the same small amount of time, no matter the list size. The total time grows because you do more comparisons, not because each one is slower.
Understanding how comparison operations add up helps you explain how searching or filtering works in real code. It shows you can think about efficiency clearly.
"What if we used a sorted list and stopped searching early? How would the time complexity change?"
Practice
Solution
Step 1: Understand the equality operator
The operator==is used to check if two values are equal in Swift.Step 2: Compare with other operators
!=means not equal,>means greater than, and<=means less than or equal to.Final Answer:
== -> Option AQuick Check:
Equality operator = == [OK]
- Confusing == with = (assignment)
- Using != to check equality
- Mixing > and < with equality
a is not equal to variable b in Swift?Solution
Step 1: Identify the not equal operator in Swift
Swift uses!=to check if two values are not equal.Step 2: Check syntax correctness
a != bis correct.a =! bis invalid syntax,!==is for identity comparison in some languages but not for value inequality in Swift, and<>is not a Swift operator.Final Answer:
a != b -> Option AQuick Check:
Not equal operator = != [OK]
- Swapping = and ! in != operator
- Using === instead of !=
- Using <> which is not valid in Swift
let x = 10 let y = 20 print(x > y)
Solution
Step 1: Understand the comparison
The code compares ifx(10) is greater thany(20).Step 2: Evaluate the comparison
Since 10 is not greater than 20, the expressionx > yis false.Final Answer:
false -> Option DQuick Check:
10 > 20 = false [OK]
- Assuming 10 > 20 is true
- Confusing > with <
- Printing variable values instead of boolean
let a = 5
let b = 10
if a = b {
print("Equal")
} else {
print("Not Equal")
}Solution
Step 1: Check the if condition syntax
The condition usesa = b, which is assignment, not comparison.Step 2: Correct operator for comparison
To compare values, Swift requires==instead of=.Final Answer:
Using = instead of == in if condition -> Option CQuick Check:
Comparison needs ==, not = [OK]
- Using = instead of == in conditions
- Thinking parentheses are mandatory in Swift if
- Confusing print and println functions
n is between 10 and 20 inclusive in Swift. Which of the following expressions correctly uses comparison operators to do this?Solution
Step 1: Understand the range condition
To check ifnis between 10 and 20 inclusive, it must be greater than or equal to 10 and less than or equal to 20.Step 2: Analyze each option
n >= 10 && n <= 20 usesn >= 10 && n <= 20, which correctly checks the inclusive range. n > 10 || n < 20 uses OR which is incorrect for range. n == 10 && n == 20 checks if n equals both 10 and 20 simultaneously, which is impossible. n != 10 && n != 20 checks if n is not 10 and not 20, which does not ensure the range.Final Answer:
n >= 10 && n <= 20 -> Option BQuick Check:
Inclusive range uses >= and <= with AND [OK]
- Using || instead of && for range
- Checking equality to both ends at once
- Using != which excludes values instead of range
Ready to prove it?
Pass the test and this pattern moves unknown
