Array unique and flip in PHP - Time & Space Complexity
We want to understand how the time needed changes when we use PHP functions to remove duplicates and flip arrays.
How does the work grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
$array = [1, 2, 2, 3, 4, 4, 5];
$unique = array_unique($array);
$flipped = array_flip($unique);
print_r($flipped);
This code removes duplicate values from an array, then swaps keys and values in the resulting array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the array twice -- once for removing duplicates, once for flipping.
- How many times: Each function loops through the array once, so two passes total.
As the array size grows, the number of steps grows roughly twice as fast because of two passes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps (2 x 10) |
| 100 | About 200 steps (2 x 100) |
| 1000 | About 2000 steps (2 x 1000) |
Pattern observation: The work grows linearly with the input size, doubling because of two separate loops.
Time Complexity: O(n)
This means the time needed grows directly in proportion to the number of items in the array.
[X] Wrong: "Using array_unique and array_flip together makes the code run in quadratic time because of nested loops."
[OK] Correct: Each function loops through the array separately, not inside each other, so the total work adds up linearly, not multiplies.
Understanding how built-in functions work under the hood helps you explain your code clearly and shows you know how performance scales with data size.
"What if we replaced array_unique with a manual loop that checks duplicates? How would the time complexity change?"
Practice
array_unique() do to an array?Solution
Step 1: Understand the purpose of
This function scans the array and removes any duplicate values, but keeps the first time each value appears.array_unique()Step 2: Compare with other options
Options B, C, and D describe different functions or behaviors. Only Removes duplicate values, keeping only the first occurrence correctly describesarray_unique().Final Answer:
Removes duplicate values, keeping only the first occurrence -> Option BQuick Check:
array_unique() removes duplicates = A [OK]
- Thinking it flips keys and values
- Assuming it sorts the array
- Believing it keeps the last occurrence
$arr in PHP?Solution
Step 1: Identify the correct function name
The PHP function to swap keys and values isarray_flip(). Other options are not valid PHP functions.Step 2: Confirm correct usage
The syntax isarray_flip($arr);which returns a new array with keys and values swapped.Final Answer:
array_flip($arr); -> Option AQuick Check:
Flip keys and values = array_flip() [OK]
- Using non-existent functions like flip_array()
- Confusing array_unique() with array_flip()
- Missing parentheses in function call
$arr = ['a' => 'apple', 'b' => 'banana', 'c' => 'apple']; $result = array_flip(array_unique($arr)); print_r($result);
Solution
Step 1: Apply
This removes duplicate values, so 'apple' at key 'c' is removed. Result: ['a' => 'apple', 'b' => 'banana']array_unique()on$arrStep 2: Apply
Keys and values swap: 'apple' becomes key with value 'a', 'banana' becomes key with value 'b'.array_flip()on the unique arrayFinal Answer:
Array ( [apple] => a [banana] => b ) -> Option AQuick Check:
Unique then flip = keys and values swapped without duplicates [OK]
- Flipping before removing duplicates
- Expecting duplicate keys in flipped array
- Confusing keys and values in output
$arr = ['x' => 'dog', 'y' => 'cat', 'z' => 'dog']; $result = array_flip($arr); print_r($result);What is the problem with this code?
Solution
Step 1: Understand
When flipping, values become keys. Duplicate values cause keys to overwrite previous ones.array_flip()behavior with duplicatesStep 2: Analyze the example
'dog' appears twice as a value. After flipping, only the last key ('z') remains for 'dog'.Final Answer:
The duplicate values cause keys to be overwritten in the flipped array -> Option DQuick Check:
Duplicate values overwrite keys when flipped [OK]
- Expecting all keys to be preserved
- Thinking array_flip causes syntax errors
- Assuming string keys cannot be flipped
$users = [101 => 'alice', 102 => 'bob', 103 => 'alice', 104 => 'carol'];You want to create a new array where usernames are keys and user IDs are values, but only keep the first occurrence of each username. Which code correctly achieves this?
Solution
Step 1: Remove duplicate usernames with
This keeps the first occurrence of each username, removing duplicates.array_unique()Step 2: Flip keys and values with
Swaps usernames to keys and user IDs to values, as required.array_flip()Final Answer:
$uniqueUsers = array_flip(array_unique($users)); -> Option CQuick Check:
Unique usernames first, then flip keys and values [OK]
- Flipping before removing duplicates
- Applying unique after flipping
- Assuming array_unique works on keys
Ready to prove it?
Pass the test and this pattern moves unknown
