Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

FInd Missing number and MIN and MAX Value and find Dublicate values count WITHOUT using PHP inbuild functions.Checked many but no suitable for me

I am having below array

$inputArray = array(1, 2, 5, 7,2);
  • Now i want to find MIN and MAX Values.
  • Second Thing find missing values in this array
  • Third dublicate number count, How can i acheive this,pls anyone
    help me here.

my code

$inputArray = array(1, 2, 5, 7,2);
$min = 0;
$max = 0;
foreach($inputArray as $inputValue){
    if ($inputValue >= $min) { 
       $max = $inputValue; // maximum value
    }
}

foreach($inputArray as $inputValue){
    if ($max >= $inputValue) { 
       $min = $inputValue; // minimum value
    }
}
echo $max;

Note: Mainthing we should not use single PHP inbuild functions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

$inputArray = array(1, 2, 5, 7,2);
$i=0;
foreach($inputArray as $inputValue){
   if($i == 0){
      $min = $inputValue;
      $max = $inputValue;
   }
   if($inputValue > $max){
      $max = $inputValue;
   }
   if($inputValue < $min){
      $min = $inputValue;
   }
   $i +=1;
}
echo $i ."<br>"; // array count

echo $min ."<br>";

echo $max ."<br>";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading