w3resource

PHP Exercises: Remove duplicates from a sorted list

PHP: Exercise-35 with Solution

Write a PHP program to remove duplicates from a sorted list.

Input: (1,1,2,2,3,4,5,5)
Output: (1,2,3,4,5)

Sample Solution: -

PHP Code:

<?php
function remove_duplicates_list($list1) {
  $nums_unique = array_values(array_unique($list1));
  return $nums_unique ;
}
$nums = array(1,1,2,2,3,4,5,5);
print_r(remove_duplicates_list($nums));
?>

Sample Output:

Array                                                       
(                                                           
    [0] => 1                                                
    [1] => 2                                                
    [2] => 3                                                
    [3] => 4                                                
    [4] => 5                                                
)             

Flowchart:

Flowchart: Remove duplicates from a sorted list.

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP program to check if the bits of the two given positions of a number are same or not.
Next: Write a PHP program to test if a given string occurs at the end of another given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

Returns all elements in an array except for the first one

Example:

<?php
function tips_tail($items)
{
  return count($items) > 1 ? array_slice($items, 1) : $items;
}

print_r(tips_tail([1, 5, 7]));
?> 

Output:

Array
(
    [0] => 5
    [1] => 7
)