Aaron Matthews
Aaron Matthews

Reputation: 128

Understanding get second lowest and second highest value in array

Good day fellow Stack-ers,

I must ask your pardon if this question has been asked before or if it seems elementary (I am only a Javascript novice).

I have been doing w3c js challenges lately: Write a JavaScript function which will take an array of numbers stored and find the second lowest and second greatest numbers.

Here is my answer:

var array = [3,8,5,6,5,7,1,9];
var outputArray = [];

function arrayTrim() {
  var sortedArray = array.sort();
  outputArray.push(sortedArray[1],array[array.length-2]);
  return outputArray;
}

arrayTrim();

and here is the answer that they have provided:

function Second_Greatest_Lowest(arr_num) {  
  arr_num.sort(function(x,y) {  
    return x-y;
  });  

  var uniqa = [arr_num[0]];  
  var result = [];  

  for(var j=1; j < arr_num.length; j++) {  
    if(arr_num[j-1] !== arr_num[j]) {
      uniqa.push(arr_num[j]);  
    }  
  }

  result.push(uniqa[1],uniqa[uniqa.length-2]);
  return result.join(',');  

}  

alert(Second_Greatest_Lowest([1,2,3,4,5])); 

I know that the for loop runs through until the length of the input, but I don't understand the if statement nested within the for loop. It seems like a long way around to the solution.

Thank you!

Upvotes: 2

Views: 5164

Answers (3)

Sumedh Ulhe
Sumedh Ulhe

Reputation: 677

import java.util.Arrays;

public class BubbleWithMax_N_Min
{
    public static void main(String[] agrs)
    {

        int temp;
        int[] array = new int[5];
        array[0] = 3;
        array[1] = 99;
        array[2] = 55;
        array[3] = 2;
        array[4] = 1;

        System.out.println("the given array is:" + Arrays.toString(array));
                for (int i = 0; i < array.length; i++)
                {
                    System.out.println(array[i] + "");
                }

                for (int i = 0; i < array.length; i++)
                {

                    for (int j = 1; j < array.length - i; j++)
                    {

                        if (array[j - 1] > array[j])
                        {

                            temp = array[j - 1];
                            array[j - 1] = array[j];
                            array[j] = temp;

                        }

                    }
                }

                System.out.println(" 2nd Min and 2nd Highest:");
                for (int i = 0; i < 1; i++)
                {
                    System.out.println(array[i+1]);

                }
        for (int i = 0; i < 1; i++)
        {
            int a= array.length-2;
            System.out.println(array[a]);

        }

            }
        }

Upvotes: 1

Jamiec
Jamiec

Reputation: 136104

The loop in question:

for(var j=1; j < arr_num.length; j++) {  
    if(arr_num[j-1] !== arr_num[j]) {
      uniqa.push(arr_num[j]);  
    }  
  }

Provides some clue as to what it's doing by using a (reasonably) descriptive variable name: uniqa - or "unique array". The if statement is checking that the current element is not the same as the previous element - having sorted the array initially this works to give you a unique array - by only filling a new array if the element is indeed unique.

Thereafter the logic is the same as yours.

Upvotes: 2

C3roe
C3roe

Reputation: 96306

Your answer does not perform correct for input such as f.e. [3,8,5,6,5,7,1,1,9]. Your proposed solution returns 1 as the second lowest number here – whereas it should actually be 3.

The solution suggested by the site takes that into account – that is what the if inside the loop is for, it checks if the current number is the same as the previous one. If that’s the case, it gets ignored. That way, every number will occur once, and that in turn allows to blindly pick the second element out of that sorted array and actually have it be the second lowest number.

It seems like a long way around to the solution

You took a short cut, that does not handle all edge cases correctly ;-)

Upvotes: 3

Related Questions