## how to find highest and second highest number in an array without using max function

I already have solution. But i think it will be more optimizable. So please provide me a solution for it. And remember that don't use predefined function of php. Like max() function. i know there are so many ways to find it but i want best and better solution. Because my array contains more than 1 lakh records and it's taking lot of time. Or sometime site will be down.

My code :

<?php $array = array('1', '15', '2','10',4); echo "<pre>"; print_r($array); echo "<pre>"; $max = 0; $s_max=0; for($i=0; $i<count($array); $i++) { $a = $array[$i]; $tmax = $max; $ts_max = $s_max; if($a > $tmax && $a > $ts_max) { $max = $a; if($tmax > $ts_max) { $s_max = $tmax; } else { $s_max = $ts_max; } } else if($tmax > $a && $tmax > $ts_max) { $max = $tmax; if($a > $ts_max) { $s_max = $a; } else { $s_max = $ts_max; } } else if($ts_max > $a && $ts_max > $tmax) { $max = $ts_max; if($a > $tmax) { $s_max = $a; } else { $s_max = $tmax; } } } echo "Max=".$max; echo "<br />"; echo "S_max=".$s_max; echo "<br />"; ?>

<?php $array = array('200', '15','69','122','50','201'); $max_1 = $max_2 = 0; for ($i=0; $i<count($array); $i++) { if ($array[$i] > $max_1) { $max_2 = $max_1; $max_1 = $array[$i]; } else if ($array[$i] > $max_2 && $array[$i] != $max) { $max_2 = $array[$i]; } } echo "Max=".$max_1; echo "<br />"; echo "Smax 2=".$max_2;

**Find Second largest element in an array,** How do you find the largest number in an array? Given an array of integers, our task is to write a program that efficiently finds the second largest element present in the array. Example: Input : arr[] = {12, 35, 1, 10, 34, 1} Output : The second largest element is 34. Input : arr[] = {10, 5, 10} Output : The second largest element is 5.

See this solution.

<?php $numbers = array_unique(array(1,15,2,10,4)); // rsort : sorts an array in reverse order (highest to lowest). rsort($numbers); echo 'Highest is -'.$numbers[0].', Second highest is -'.$numbers[1]; // O/P: Highest is -15, Second highest is -10 ?>

**Program to find largest element in an array,** Given an array of integers, our task is to write a program that efficiently finds the second In the second traversal find the greatest element less than the element obtained in first traversal. the value of current variable as second = arr[i] 3) Return the value stored in second. Function to print the second largest elements */. The above code has been tested with integer arrays having duplicate entries, negative values. Largest number and second largest number are retrived in one pass. This code only fails if array only contains multiple copy of same number like {8,8,8,8} or having only one number.

I didn't check your solution, but in terms of complexity it's IMO optimal. If the array has no more structural information (like it's sorted) there's no way to skip entries. I.e. the best solution is in O(n) which your solution is.

**Find Second Largest Number in an Array (Multiple Approaches ,** The solution is to initialize max as first element, then traverse the given array from second element till end. C function to find maximum in arr[] of size n. For example: if we given 7 input numbers as 65, 76, 54, -98, 35, 23, 93, then the largest of 7 numbers without using Array is 93. The below given C program will find the largest (biggest, greatest or highest or maximum) of n numbers.

The answer given by "Kanishka Panamaldeniya" is fine for highest value but will fail on second highest value i.e. if array has 2-similar highest value, then it will showing both Highest and second highest value same. I have sorted out it by adding one more level comparsion and it works fine.

$array = array(50,250,30,250,40,70,10,50); // 250 2-times $max=$max2=0; for ($i = 0; $i < count($array); $i++) { if ($array[$i] > $max) { $max2 = $max; $max = $array[$i]; } else if (($array[$i] > $max2) && ($array[$i] != $max)) { $max2 = $array[$i]; } } echo "Highest Value is : " . $max . "<br/>"; //output : 250 echo "Second highest value is : " . $max2 . "<br/>"; //output : 70

**How to find the second largest number among hundreds of numbers ,** In this tutorial, I have explained a java code to find second highest number in an array without Duration: 8:37
Posted: Jul 5, 2019 I have an integer array like this : arr[20,120,111,215,54,78]; I need a function taking an array as its argument and returning the second largest element of that array.

This code will return second max value from array

$array = array(80,250,30,250,40,90,10,50,60,50); // 250 2-times $max=$max2=0; for ($i = 0; $i < count($array); $i++) { if($i == 0) { $max2 = $array[$i]; } if($array[$i] > $max) { $max = $array[$i]; } if($max > $array[$i] && $array[$i] > $max2) { $max2 = $array[$i]; } } echo "Highest Value is : " . $max . "<br/>"; //output : 250 echo "Second highest value is : " . $max2 . "<br/>"; //output : 90

**1: How to find a second larges,** How do I find the second largest number among hundreds of numbers entered without using an array, using functions? Find max of matrix without using built in Learn more about max, matlab function, homework

**PHP: max,** 1: How to find a second largest number within an array without using any loop 3) Now again put max() function on third array which we got from array_diff() To extract the largest number in a group of numbers, supply that group to the MAX function as a range reference. A range can contain as many rows and columns as you desire. For example, to get the highest value in the range C2:E7, use this simple formula: Find highest value in non-adjacent cells or ranges. To make a MAX formula for non

**Find out the second highest in array,** If the first and only parameter is an array, max() returns the highest value in that array. all max functions i see around don't provide a way to get the max value of the Matlab users and others may feel lonely without the double argument output from min and max functions. $array = array( 0 => 'first', 1=> 'second', /* . This final array goes to the MAX function and it returns the largest number. The second formula evaluates the same conditions within a single logical test and the multiplication operation works like the AND operator: When the TRUE and FALSE values are used in any arithmetic operation, they are converted into 1's and 0's, respectively.

**Finding largest numbers in array - PHP,** There is a problem in your code. Assuming that input is [3, 2, 1] , the program will work like this. Set first and second as 3. Iterate through elements noticing that Given a list of numbers, the task is to write a Python program to find the second largest number in given list. Examples: Input : list1 = [10, 20, 4] Output : 10 Input : list2 = [70, 11, 20, 4, 100] Output : 70