Finding the Second Highest Number in Array

Method to find second highest number in an array in java

You could change the array parameter name arr and remove the declaration or copy the values from a to arr.

public static int second(int arr[],int n) {
int temp;
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr[n-2];
}

The reason you get zero is because the primitive int cannot be null. So, when you create the array of length 5, it starts out filled with zeroes.

How to find second highest number in ArrayList of Objects value

public float secondHighest(ArrayList<Float> array){
float highest = 0;
for(float i : array){
if(i > highest) highest = i;
}
float secondHighest = 0;
for(float i : array){
if(i > secondHighest && i < highest) secondHighest = i;
}
return secondHighest;
}

javascript function to find the second largest element in an array

Just one minor change:

Use nums[j]<largest instead of large<largest in the second for loop

function getSecondLargest(nums) {    // Complete the function    var largest=nums[0];    for(let i=1;i<nums.length;++i)    {        if(nums[i]>largest)        largest=nums[i];    }    var large;        //To ensure that the selected number is not the largest    for(let j=0;j<nums.length;++j)    {        if (nums[j] !== largest){            large = nums[j];            break;        }    }    for(let j=1;j<nums.length;++j)    {        if(large<nums[j]&&nums[j]!=largest)            large=nums[j];        else                    console.log(large)    }    return large;}
var secondLargest = getSecondLargest([6,3,6,6,5]);console.log("Second largest number", secondLargest);

Find 2nd largest value in an array that has duplicates of the largest integer

You can simply create a Set first and than sort in descending and take the 1st index element

let array = [0, 3, 2, 5, 5]
let op = [...new Set(array)].sort((a,b) => b-a)[1]
console.log(op)

finding the second largest value in an array using java

your mistake is the conditions in the loop
use this code:

public class Main{
public static void main(String[] args){
int[] nums = { 6, 9, 11, 1, 10 };

int max = Integer.MIN_VALUE;
int secmax = Integer.MIN_VALUE;

for(int x=0; x<nums.length; x++) {
if(nums[x]>max ) {
secmax = max;
max=nums[x];
}else if(nums[x]>secmax){
secmax=nums[x];
}
}
System.out.println("1st H value: " + max);
System.out.println("2nd H Value: " + secmax);
}
}

Using reduce() to find the second largest element in the array

Yes there are several ways you can do this. Here is one solution:

const array1 = [5, 5, 1, 1, 2, 3, 4];
const array2 = [12,16,1,5];
const reducer = (accumulator, currentValue, idx, a) => {
let larger = a.filter(n=>n>currentValue) // Fetch larger values than n
.filter((n,i,a) => a.indexOf(n) === i); // Get Unique values
if(larger.length === 1){
// if there is only one unique value larger than n, n is your answer.
return accumulator = currentValue;
} else {
return accumulator = accumulator; // else preserve the accumulator value
}
}
console.log(array1.reduce(reducer));
console.log(array2.reduce(reducer));

How do I get the second largest element from an array in javascript

Original answer

var secondMax = function (){ 
var arr = [20, 120, 111, 215, 54, 78]; // use int arrays
var max = Math.max.apply(null, arr); // get the max of the array
arr.splice(arr.indexOf(max), 1); // remove max from the array
return Math.max.apply(null, arr); // get the 2nd max
};

demo

Update 1

As pointed out by davin the performance could be enhanced by not doing a splice but temporarily replacing the max value with -Infininty:

var secondMax = function (arr){ 
var max = Math.max.apply(null, arr), // get the max of the array
maxi = arr.indexOf(max);
arr[maxi] = -Infinity; // replace max in the array with -infinity
var secondMax = Math.max.apply(null, arr); // get the new max
arr[maxi] = max;
return secondMax;
};

Anyway, IMHO the best algorithm is Jack's. 1 pass, with conversion to number.
Mine is just short, using builtin methods and only wanted to provide it as an alternative, to show off all the different ways you can achieve the goal.

Update 2

Edge case with multiple values.

As comments pointed it out: this solution "does not work" if we have an array like [3, 3, 5, 5, 5, 4, 4].
On the other hand it would be also a matter of interpretation what we would consider "the 2nd largest element".
In the example we have:

  1. 3 elements with the largest value (5) at indices: 2,3,4
  2. 2 elements with the second largest value (4) at indices: 5,6
  3. 2 elements with the second smallest value (3) at indices: 1,2

The 2nd largest element could be interpreted as:

  1. the 2nd (largest element) - 5 at index 3 - assuming that there is an order, and that we aim for a unique value
  2. the (2nd largest) element - 4 at index 5 - assuming that there is an order, and that we aim for a unique value


Related Topics



Leave a reply



Submit