PHP | Remove duplicate elements from Array

You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array.

Examples:

Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3}
Output : array (
                [6] => 2
                [7] => 3
                [4] => 1
                [5] => 6
               )

Input : array[] = {4, 2, 7, 3, 2, 7, 3}
Output : array (
                [0] => 4
                [4] => 2
                [5] => 7
                [6] => 3 
               )

In C/Java, we have to traverse the array and for each element you have to check if its duplicate is present. But PHP provides an inbuilt function (array_flip()) with the use of which we can remove duplicate elements without the use of loop.



The array_flip() returns an array in flip order, i.e. keys from array become values and values from array become keys.

Note that the values of array need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result.

Note: If a value has several occurrences, the latest key will be used as its value, and all others will be lost. Also as array_flip() returns array with preserved keys, we will use array_values() which will re-order the keys.

Approach: The idea is to use this property of the array_flip() function of selecting the latest key as its value if the value has multiple occurrences. We will use the array_flip() function twice to remove the duplicate values. On using the array_flip() function for the first time, it will return an array with keys and values exchanged with removed duplicates. On using it the second time, it will again reorder it to the original configuration.

Below is the implementation of above idea:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    // define array
    $a = array(1, 5, 2, 5, 1, 3, 2, 4, 5);
      
    // print original array
    echo "Original Array : \n";
    print_r($a);
     
    // remove duplicate values by using 
    // flipping keys and values
    $a = array_flip($a);
   
    // restore the array elements by again 
    // flipping keys and values.
    $a = array_flip($a);
  
    // re-order the array keys
    $a= array_values($a);
  
    // print updated array
    echo "\nUpdated Array : \n ";
    print_r($a);
?>

chevron_right


Output:

Original Array : 
Array
(
    [0] => 1
    [1] => 5
    [2] => 2
    [3] => 5
    [4] => 1
    [5] => 3
    [6] => 2
    [7] => 4
    [8] => 5
)

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.