Find out Maximum and Minimum number from an array. Create an Unique Array and count no of each repeat elements in an array

 Find out Maximum and Minimum number from an array. Create an Unique Array and count no of each repeat elements in an array.

STEP 1: First we need to declare an array of our own size with duplicate values as the array size is not mentioned to create an unique array and to count the number of repeat elements in an array.

$arr=[22,4,10,13,13,13,27,92,12,20,20]; 

STEP 2: Then to find the maximum and minimum numbers from an array we need to declare two variables as per our choice where we need to assign the array with max() function and min() function in that two variables like here I have assign the max() function of $arr in $max variable and also I have assign the min() function of $arr in $min variable where $ symbol is the super global variable and then I have print the output of maximum and minimum number using print_r().

NOTE: Super global variable is the pre-defined variable which is accessible from any function, class or file.

$max=max($arr);

$min=min($arr);

print_r("The maximum number from an array is ".$max."<br>");

print_r("The minimum number from an array is ".$min."<br>"); 

STEP 3: Then to create an unique array I have used array_unique() function whose function is to remove the duplicate values from an array and keep one one array values and all other duplicate array values will be removed.

$uniarr=array_unique($arr);

print_r($uniarr);

STEP 4: Then to count the number of repeat elements in an array I have used the function array_count_values where it will count the time of the values I have used in an array and it will count the time of the duplicate values also I have used in an array.

$arrcnt=array_count_values($arr);

print_r($arrcnt);

CODE:

<?php

$arr=[22,4,10,13,13,13,27,92,12,20,20];

$max=max($arr);

$min=min($arr);

print_r("The maximum number from an array is ".$max."<br>");

print_r("The minimum number from an array is ".$min."<br>");

$uniarr=array_unique($arr);

$arrcnt=array_count_values($arr);

print_r($uniarr);

echo "<br>";

print_r($arrcnt);

?>

OUTPUT: 

The maximum number from an array is 92
The minimum number from an array is 4
Array ( [0] => 22 [1] => 4 [2] => 10 [3] => 13 [6] => 27 [7] => 92 [8] => 12 [9] => 20 )
Array ( [22] => 1 [4] => 1 [10] => 1 [13] => 3 [27] => 1 [92] => 1 [12] => 1 [20] => 2 )






 

Comments

Popular posts from this blog

Take 4 arrays then margin the arrays using array_combine function (PHP Array Functions).

My Notes On Some Basic Interview Questions Based On PHP