Remove Duplicates with array_unique()
Write a PHP function that takes an array and returns a new array with duplicate values removed using array_unique(). Only the first occurrence of each value is kept.
array_unique() is a PHP function that takes an array and returns a new array with duplicate values removed. It compares values as strings by default (using (string) casting), but can use a comparison flag like SORT_NUMERIC or SORT_REGULAR.
The function preserves the original keys of the first occurrence of each value. Use array_values() after array_unique() if you need re-indexed numeric keys.
Time complexity is O(n log n) as it sorts the array internally to find duplicates. For large arrays, consider using array_count_values() for frequency-based operations.
Example Input & Output
Empty array
String duplicates
Remove duplicates
Already unique
All same
Algorithm Flow

Solution Approach
Best Answers
<?php
function solution($arr) {
return array_values(array_unique($arr));
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
