0
0
PhpProgramBeginner · 2 min read

PHP Program to Remove Duplicates from Array

You can remove duplicates from an array in PHP using the built-in function array_unique(), for example: $uniqueArray = array_unique($array);.
📋

Examples

Input[1, 2, 2, 3, 4, 4, 5]
Output[1, 2, 3, 4, 5]
Input['apple', 'banana', 'apple', 'orange']
Output['apple', 'banana', 'orange']
Input[]
Output[]
🧠

How to Think About It

To remove duplicates from an array, think of checking each item and keeping only the first time it appears. PHP has a built-in function array_unique() that does this automatically by scanning the array and removing repeated values, leaving only unique ones.
📐

Algorithm

1
Get the input array.
2
Use a function to scan the array and keep only unique values.
3
Return or print the new array without duplicates.
💻

Code

php
<?php
$array = [1, 2, 2, 3, 4, 4, 5];
$uniqueArray = array_unique($array);
print_r($uniqueArray);
?>
Output
Array ( [0] => 1 [1] => 2 [3] => 3 [4] => 4 [6] => 5 )
🔍

Dry Run

Let's trace the array [1, 2, 2, 3, 4, 4, 5] through the code.

1

Input array

The array is [1, 2, 2, 3, 4, 4, 5].

2

Apply array_unique

The function scans and removes repeated values, keeping only the first occurrence.

3

Resulting array

The resulting array is [1, 2, 3, 4, 5] with original keys preserved.

Original ArrayAfter array_unique
[1, 2, 2, 3, 4, 4, 5][1, 2, 3, 4, 5]
💡

Why This Works

Step 1: Using array_unique

The array_unique() function scans the array and removes duplicate values, keeping only the first occurrence.

Step 2: Preserving keys

This function preserves the original keys of the first occurrences, which can be useful if keys matter.

Step 3: Output with print_r

Using print_r() shows the array structure clearly, including keys and values.

🔄

Alternative Approaches

Using a loop and a temporary array
php
<?php
$array = [1, 2, 2, 3, 4, 4, 5];
$uniqueArray = [];
foreach ($array as $value) {
    if (!in_array($value, $uniqueArray)) {
        $uniqueArray[] = $value;
    }
}
print_r($uniqueArray);
?>
This method manually checks each value and adds it if not already present; it is less efficient but shows the logic clearly.
Using array_flip twice
php
<?php
$array = [1, 2, 2, 3, 4, 4, 5];
$uniqueArray = array_keys(array_flip($array));
print_r($uniqueArray);
?>
This method flips keys and values twice to remove duplicates but resets keys to numeric indexes.

Complexity: O(n) time, O(n) space

Time Complexity

The function scans each element once, so the time grows linearly with the array size.

Space Complexity

It creates a new array to store unique values, so extra space grows with the number of unique elements.

Which Approach is Fastest?

array_unique() is the fastest and simplest built-in method; manual loops are slower and more code.

ApproachTimeSpaceBest For
array_unique()O(n)O(n)Quick and easy removal of duplicates
Manual loop with in_array()O(n^2)O(n)Learning logic but slower for large arrays
array_flip twiceO(n)O(n)Removes duplicates but resets keys
💡
Use array_unique() for a quick and easy way to remove duplicates from arrays in PHP.
⚠️
Beginners often forget that array_unique() preserves keys, which can cause unexpected keys in the result.