PHP | Remove duplicate elements from Array
Last Updated :
30 Mar, 2018
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:
php
<?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);
?>
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
)
Similar Reads
Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r
3 min read
How to Remove duplicate elements from array in JavaScript ? In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index
4 min read
How to Remove Duplicate Elements from an Array using Lodash ? Removing duplicate elements from an array is necessary for data integrity and efficient processing. The approaches implemented and explained below will use the Lodash to remove duplicate elements from an array. Table of Content Using uniq methodUsing groupBy and map methodsUsing xor functionUsing un
3 min read
Remove duplicate elements from an array using AngularJS We have given an array and the task is to remove/delete the duplicates from the array using AngularJS.Approach: The approach is to use the filter() method and inside the method, the elements that don't repeat themselves will be returned and the duplicates will be returned only once.Hence, a unique a
2 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read