Finding largest numbers in array

I had a question in a interview which was tricky, the question was to print 2 largest number in a aray without using sort function e.g
How do i do it using php? Only way i can think of strlen or max function but max will return one value only.

For the second max value, I would do:

$l = [1,5,19,200,999];
$n = max(array_diff($l, [max($l)]));

That is: calculating the max of an array that will not contain the max value. Or removing the max value of the array, and then calculating the max value.

That just echo the second last, whereas i want both like

The last one you already have it. It’s max($l).

So you have:

$l = [1,5,19,200,999];
$last = max($l);
$second = max(array_diff($l, [$last]));

echo $second;
echo $last;

@joksnet Thanks, i need to polish my array functions. Is there anyway to get the same result using strlen?

I don’t see how strlen could be used reliably with numerics. eg. which is the larger?
99 or 3.14 → 3.14
45 or 78 → equal

Function strlen will not tell you if a number if grater than another. What happen with strlen(200) and strlen(999), both gives 3 and now what?

You could resolve this exercise with an iteration if the above seems too odd. Try it.

Here is fun one:

while (count($l) > 2) {
  $k = array_flip($l);

This one expects that are not duplicated numbers.

@joksnet thanks for explaining about strlen too. Can you breakdown the $second statement here, as its overwhelming for me.

No problem.

We start with our list that’s $l = [1,5,19,200,999], if we do max($l) we get 999. Then we build a new list with only 999 as the unique element; let’s call it $p = [999]. Then we are going to compare the first list ($l) with the second list ($p) using array_diff; this does is to generate a new list with all items in the first list that are NOT in the second list. So, the new list will contain $q = [1,5,19,200] and won’t contain 999 because it exists in the second list ($p). Finally we get the $n = max($q) that is second maximum value of the original list, in this case 200.

In human terms, what array_diff([1,5,19,200,999], [999]) does is to remove the 999 element from the original list.

I might not be very good at explaining. Hope this helps.

you explained it extra good.Thank you! But wondering why you used in array_diff(array1,[array2])

[p.s] - would you make it to a interview in which your just given a paper and pen with same question and you have to write the answer? Without using computer?!

array_diff compares two arrays so you need to pass it two arrays and not one array and a number.

Here’s a completely different approach, in psuedocode.

Record the highest value seen so far.

max = 0; // None so far.

Iterate through the array, checking each value.

if (val > max)
max = val;

When you are finished, you have the largest value.
The advantage here over using the built-in max function is that you only need to go through the array once. Just record the two highest values as you go.
Finding the second highest value is left as an exercise for the reader.


But much more slowly than using the built in functions that use compiled code rather than having to interpret the assignment every time around the loop.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.