Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,954 questions

51,896 answers

573 users

How to find third largest number in an array with Java

1 Answer

0 votes
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.Collectors;
 
public class MyClass {
 
    public static int ThirdLargest(int[] arr) {
        List<Integer> integerList = Arrays.stream(arr).boxed().
                                    sorted(Collections.reverseOrder()).
                                    collect(Collectors.toList());
         
        return integerList.get(2);
    }
 
    public static void main(String[] args) {
        int[] arr = new int[]{34, 3, 8, 2, 9, 4, 6, 12, 7};

        System.out.print(ThirdLargest(arr));
    }
}
 
 
 
 
/*
run:
 
9
 
*/

 

 



answered Mar 17, 2023 by avibootz

Related questions

2 answers 73 views
1 answer 140 views
1 answer 96 views
1 answer 101 views
2 answers 205 views
1 answer 172 views
1 answer 153 views
...