xxxxxxxxxx
def find_second_largest(arr):
if len(arr) < 2:
print("Array should have at least two elements.")
return
largest = second_largest = float('-inf')
for num in arr:
if num > largest:
second_largest = largest
largest = num
elif largest > num > second_largest:
second_largest = num
return second_largest
# Example usage:
numbers = [5, 10, 9, 12, 15, 3]
result = find_second_largest(numbers)
print("Second largest number:", result)
xxxxxxxxxx
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
};
xxxxxxxxxx
['20','120','111','215','54','78'].sort(function(a, b) { return b - a; })[1];
// '120'
xxxxxxxxxx
// [10,30,15,4,2,5]
// when you get element< smallest
//interchange second_smallest <-> smallest
//smallest= element
//else if u get element< second_smallest
//if element != smallest then
//second_smallest= element
//find neat code below
long min=Integer.MAX_VALUE;
long sMin= Integer.MAX_VALUE;
for(int i=0; i<n; i++){
if(a[i]< min){
sMin=min;
min= a[i];
}
else if(a[i]<sMin && a[i]!= min){
sMin= a[i];
}
}
xxxxxxxxxx
public static void SecondndLargestfromarray()
{
var scan = new Scanner(System.in);
var n = scan.nextInt();
long l=Long.MIN_VALUE;
long p=Long.MIN_VALUE;
for (int i = 0; i < n; i++) {
var sc = scan.nextLong();
if(sc>l)
{
p=l;
l=sc;
}
else if(sc>p ) p = sc;
}
System.out.println(p);
}
xxxxxxxxxx
import java.util.Random;
public class secondBiggestNumber {
public static void main (String[] args){
// creating array
int[] array1 = new int[8];
// randomly filling with 8 elements
fill(array1, -100, 100);
// printing them out
for (int i = 0; i < array1.length ; i++) {
System.out.println(array1[i] + " ");
}
//sorting them by ascending order
int current = 0;
for (int i = 0; i < array1.length ; i++) {
for (int j = i+1; j < array1.length ; j++) {
if (array1[i]>array1[j]) {
current = array1[i];
array1[i] = array1[j];
array1[j] = current;
}
}
}
//printing out the second biggest number
System.out.println("The second biggest number: " + array1[6] );
}
private static void fill(int[] array, int lowerBound, int highBound) {
Random random = new Random();
int bound = highBound - lowerBound;
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(bound) + lowerBound;
}
}
}
xxxxxxxxxx
/* C program to print second largest element of the array*/
//Save it as SecondLargestElementArray.c
#include<stdio.h>
#include<limits.h>
int main(){
int i,n;
printf("Enter the size of array : ");
scanf("%d",&n);
//Declaring array
int arr[n];
printf("Enter the elements of the array : ");
for(i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
//Declaring maximum element
int maximum = INT_MIN;
//Comparing with each element and find maximum element
for(i=0;i<n;i++) {
if(arr[i] > maximum) {
maximum = arr[i];
}
}
printf("The maximum value is : %d", maximum);
int second_max = INT_MIN;
//Finding Second largest element.
//Comparing with each element and also checking it is not equal to max
for(i=0;i<n;i++) {
if(arr[i] > second_max && arr[i]!=maximum) {
second_max = arr[i];
}
}
printf("\nThe second maximum value is : %d", second_max);
}
Input:
Enter the size of array :
6
Enter the elements of the array :
4 3 8 0 8 7
Output:
The maximum value is : 8
The second maximum value is : 7
xxxxxxxxxx
public static void main()
{
a1(new int[]{1, 2, 3, 4});
a1(new int[]{4, 1, 2, 3});
a1(new int[]{1, 1, 2, 2});
a1(new int[]{1, 1});
a1(new int[]{1});
a1(new int[]{});
}
static int a1(int[] a)
{
int max1 = -1;
int max2 = -1;
for (int i=0; i<a.length; i++)
{
if (a[i] > max1)
{
max2 = max1;
max1 = a[i];
}
else if (a[i] != max1 && a[i] > max2)
max2 = a[i];
}
return max2;
}
xxxxxxxxxx
public static int secHigh(int arr[]){
int firstHigh = 0,secHigh = 0;
for(int x: arr){
if(x > firstHigh){
secHigh = firstHigh;
firstHigh = x;
}else if(x > secHigh){
secHigh = x;
}
}
return secHigh;
}
xxxxxxxxxx
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;
};
xxxxxxxxxx
function largestOfFour(mainArray) {
return mainArray.map(function (subArray){
return subArray.reduce(function (previousLargestNumber, currentLargestNumber) {
return (currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber;
}, 0);
});
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);