How to Print Unique Elements an Given Array in PHP?
Last Updated :
03 Jul, 2024
Given an array, the task is to print all distinct elements of the given array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted.
Examples:
Input: arr[] = {5, 6, 5, 3, 6, 4, 3, 5}
Output: 5, 6, 3, 4
Input: arr[] = {1, 2, 3, 4, 5, 5, 4, 3, 3, 3}
Output: 1, 2, 3, 4, 5
There are two methods to solve this problem, these are:
Using array_unique() Function
The array_unique() function is used to remove the duplicate values from an array. If there are multiple elements in the array with same values then the first appearing element will be kept and all other occurrences of this element will be removed from the array.
Syntax:
array array_unique($array , $sort_flags)
Example:
PHP
<?php
$array = [1, 1, 3, 3, 3, 4, 4, 5, 7];
$printUniqueElements = array_unique($array);
print_r($printUniqueElements);
?>
OutputArray
(
[0] => 1
[2] => 3
[5] => 4
[7] => 5
[8] => 7
)
Using Nested Loop
We can print unique elements using nested loop. The outer loop picks an element one by one starting from the leftmost element. The inner loop checks if the element is present on left side of it. If present, then ignores the element, else prints the element.
Example:
PHP
<?php
function printUniqueElements($arr, $len) {
for($i = 0; $i < $len; $i++) {
$j = 0;
for($j; $j < $i; $j++) {
if ($arr[$i] == $arr[$j])
break;
}
if ($i == $j)
echo $arr[$i] , " ";
}
}
// Driver function
$arr = [12, 10, 9, 45, 2, 10, 10, 45];
$len = sizeof($arr);
printUniqueElements($arr, $len);
?>
Using a Hash Table
A hash table (or dictionary in some languages) allows for efficient insertion and lookup operations. By leveraging a hash table, we can ensure each element is processed in constant time, making the approach both time-efficient and straightforward.
Example:
PHP
<?php
function printUniqueElements($arr) {
$seen = array(); // Hash table to keep track of seen elements
foreach ($arr as $value) {
if (!in_array($value, $seen)) {
echo $value . " ";
$seen[] = $value;
}
}
}
// Driver function
$arr = [12, 10, 9, 45, 2, 10, 10, 45];
printUniqueElements($arr);
?>
Similar Reads
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra
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
How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function
5 min read
How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i
4 min read
Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
2 min read