write a java program to find second largest element in the array in the java

preview_player
Показать описание
#learnwithkrishnasandeep #javacodinginterviewquestions #javaexamples #javaprograms #javatutorials #javaprogramming This tutorial explains how to find second largest element in array in java.
Рекомендации по теме
Комментарии
Автор

what if there is a repetition of the highest element in the array then it will print the highest element only

kartiktyagi
Автор

what if this situation comes -
int arr[ ] = {1, 2, 3, 4, 5, 6, 6};
please answer.
thank you!

deependrasingh
Автор

Thanks bro.I understood ur explanation simply.thankd a lot

swathik
Автор

Wow So simple....some more programs pls

GauravSharma-eief
Автор

we can sort the array using bubble sort algorithm which is the simplest one if you dont want to use sort function

digvijaysingh
Автор

love you bro good content made it really easy for understing

LalitKumar-cwrf
Автор

nice one but there is another method of finding second largest number by taking two variables and that is much better then this method, anyways good going, keep it up sir, thank you 🙂🙂🙂🙂👍👍👍

SmartProgramming
Автор

without using sort() method or any in-built function

tanujkumar
Автор

with out taking values initially how can we solve ?

Akhil
Автор

Hi,

If we don't know the position then how will be the approach.

for ex: we take value as desired.

Regards,
Vishwaa.

vishwaprasad
Автор

public class MyClass {
public static void main(String args[]) {
int arr[]={4, 2, 1, 5, 3};
int sl, l;
l=sl=Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++)
{
if(l<arr[i])
{
sl=l;
l=arr[i];
}
else if(arr[i]>sl && arr[i]<l)
{
sl=arr[i];
}
}
if(sl==l)
{
System.out.println("There is no 2nd largest number");
}
else
{
System.out.println("The highest number is "+l+"The 2nd largest number is "+sl);
}
}
}

apurbapradhan
Автор

package PracticeJava;

public class SecondHighestNumber {

public static void main(String[] args) {
// TODO Auto-generated method stub

int num[]= {23, 45, 2, 4, 11, 63, 32, 0, 4, 55, 50, 89, 57};
SecondHighestNum(num);


}

public static void SecondHighestNum(int num[])
{
int result =0;

for(int i=0;i<num.length;i++)
{
for(int j=1;j<(num.length);j++)
{
if(num[j-1]>num[j]) {

result=num[j-1];
num[j-1]=num[j];
num[j]=result;
}
}

}

}

}

mudasir
Автор

To find second largest element in array in java we can use SortedSet.
Here is the complete solution.

import java.util.SortedSet;
import java.util.TreeSet;

public class Abc1 {
public static void main(String[] args) {

int[] a = { 1, 5, 4, 2, 8, 1, 8, 9, 9, 100, 23, 66, 99 };
SortedSet<Integer> set = new TreeSet<Integer>();
for (int i: a) {
set.add(i);
}
System.out.println("set => " +set);
// Remove the maximum value; print the largest remaining item
set.remove(set.last());

System.out.println("second largest after removing the largest => " + set.last() );

}
}

soudiptadutta
Автор

if we have duplicated number for ex{2, 5, 6, 9, 9, 9};

ShivKumar-yvti
Автор

this code will fail if there are repeated characters, in such cases we need a set to filter data, here is the code public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5, 7, 6, 6, 7 };
Set<Integer> set = new TreeSet<>();
for (Integer integer : arr) {
set.add(integer);
}
Integer[] array = set.toArray(new Integer[set.size()]);
int secondLargest = array[array.length - 2];

}

aayushjoshi