Java Program to Find the Second Highest Number in an Array

Поделиться
HTML-код
  • Опубликовано: 10 дек 2024

Комментарии • 65

  • @soudiptadutta6886
    @soudiptadutta6886 6 лет назад +6

    Using SortedSet we can easily get the 2nd highest in the array.
    SortedSet set = new TreeSet();
    for( int i : a) {
    set.add(i);
    }//for
    set.remove(set.last());
    System.out.println(set.last());

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  5 лет назад +2

      You can do it, but the space complexity of this approach is O(n).

    • @yagami-light
      @yagami-light 4 года назад

      Lol, what would you do if user enter multiple same elements, don't be jugaadu ...

    • @vinod3451
      @vinod3451 2 года назад

      @Ero-Sennin TreeSet does not store duplicate elements

  • @vigneshk3727
    @vigneshk3727 3 года назад +3

    I am also do this mistake in my interview so I got only 8 out of 10. Now after see your video I change my methodology to your second method sir thank you lot sir ...

  • @srgsfranco
    @srgsfranco 4 года назад +3

    Thank you for share that! I'm new to programming and I was stucked in a similar problem, where I needed to find the second highest number inputed by the user, after watching you I noticed my mistake.

  • @MohamedAneesMK
    @MohamedAneesMK 4 месяца назад +1

    What is the name of the font ??

  • @jaishankargothandaraman7998
    @jaishankargothandaraman7998 2 года назад +1

    Just curious, Do we really need the if condition (line 39 & 40) ?
    It works without that too.

  • @raam2508
    @raam2508 5 лет назад +3

    Any example , to have the second conditional check in place?

  • @ProgrammingTutorials1M
    @ProgrammingTutorials1M  5 лет назад

    Find Second Highest Number in an array (NEW VIDEO) : ruclips.net/video/Mv8jhYQEbkA/видео.html

  • @somendraprakash979
    @somendraprakash979 3 года назад +1

    thanks for the great video, keep the good work going, having one doubt , what would be scenarios if all the elements of the given unsorted array is same or the array contains only one element?

    • @SumitNegiTech
      @SumitNegiTech 2 года назад

      you already gave answer in your question just use those scenarios in if condition

  • @Champion-tt7gm
    @Champion-tt7gm 7 лет назад +1

    i dont get the Min_Value. What is its use? since we want the maximum values.Kindly explain

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  7 лет назад +1

      INT_MIN is the minimum integer value assigned to a variable. It is useful for comparing the value with other element.

    • @08ravikumar
      @08ravikumar 7 лет назад +1

      no need to use INT_MIN, instead of that just use array[0]

  • @This__is__akash
    @This__is__akash 3 года назад +1

    awesome and clean explanation..Thank you!

  • @SumitNegiTech
    @SumitNegiTech 2 года назад +1

    hey this is the the better approach the GeekforGeeks

  • @shivamkumar-qs7er
    @shivamkumar-qs7er 5 лет назад

    Amazing Variant ways!

  • @rohitrana6743
    @rohitrana6743 2 года назад

    Can you pass just 1 element in array and check the program.

  • @k.himaja9310
    @k.himaja9310 3 года назад

    Does this work if array has duplicates???

  • @patel5532
    @patel5532 2 года назад

    Sir, could you give some solution without sorting the array? because sorting will increase the time complexity.

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  2 года назад

      In this tutorial, I have explained this approach without sorting an array. Code link - webrewrite.com/find-second-smallest-number-in-an-array-java-code/

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  2 года назад

      Second highest - webrewrite.com/java-program-find-second-highest-number-array/

  • @kuldeepsahoo1602
    @kuldeepsahoo1602 6 лет назад +1

    You didn't tell about MIN_VALUE

  • @goaheadskinit
    @goaheadskinit 2 месяца назад

    Thank you

  • @himanshubagga6924
    @himanshubagga6924 2 года назад +1

    Thanks !!

  • @Sailu2
    @Sailu2 5 месяцев назад

    Thanks sir!!!!

  • @Real_Indian_bhartiya
    @Real_Indian_bhartiya Год назад

    for [10,10,10] not working , should be -1

  • @revanthreddy3838
    @revanthreddy3838 4 года назад

    What is Min_value

  • @rashwathj9696
    @rashwathj9696 Год назад

    Thank you sir

  • @shreyanjaysingh4270
    @shreyanjaysingh4270 8 месяцев назад

    Thanks

  • @chandana7277
    @chandana7277 10 месяцев назад

    tqq sir

  • @atheerahramesh4668
    @atheerahramesh4668 Год назад

    Why do u sort the array...could u explain

    • @4a5pradeep52
      @4a5pradeep52 6 месяцев назад

      when u made sorting the values are arranged in ascending order then obsoviuly ur largest number at last index position thats why we are sorting

  • @ahnafshariyar6273
    @ahnafshariyar6273 4 года назад

    How to get third highest number?

  • @hari7027
    @hari7027 2 года назад

    if array is 10,5,10 then it will fail

  • @pratikshaonkar4273
    @pratikshaonkar4273 5 месяцев назад

    Kindly speak louder.

  • @shwetachoudhary1314
    @shwetachoudhary1314 3 года назад

    voice is very low

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  3 года назад

      Sorry for that, You'll never face this issue in my new videos.

    • @nishubad15
      @nishubad15 3 года назад

      @@ProgrammingTutorials1M what is the approach for 3rd highest number

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  3 года назад

      @@nishubad15 For this you have to use heap data structure.

  • @saikumar-io7un
    @saikumar-io7un 2 года назад

    u r voice is very lowwwwwwwwwww

  • @pitanihemanth1789
    @pitanihemanth1789 7 лет назад

    nice