
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
To find largest, smallest, second largest, second smallest in an array, the code is as follows -
Example
import java.util.*; public class Demo { public static void main(String []args){ int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12}; System.out.println("Array = "+Arrays.toString(arr)); Arrays.sort(arr); System.out.println("Sorted Array = "+Arrays.toString(arr)); System.out.println("Smallest element = "+arr[0]); System.out.println("2nd Smallest element = "+arr[0]); System.out.println("Largest element = "+arr[9]); System.out.println("2nd Largest element = "+arr[8]); } }
Output
Array = [55, 10, 8, 90, 43, 87, 95, 25, 50, 12] Sorted Array = [8, 10, 12, 25, 43, 50, 55, 87, 90, 95] Smallest element = 8 2nd Smallest element = 8 Largest element = 95 2nd Largest element = 90
Example
Let us now see another example:
import java.util.*; public class Demo { public static void main(String []args){ int a; int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12}; System.out.println("Array = "+Arrays.toString(arr)); int count = arr.length; for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (arr[i] > arr[j]) { a = arr[i]; arr[i] = arr[j]; arr[j] = a; } } } System.out.println("Smallest: "+arr[0]); System.out.println("Largest: "+arr[count-1]); System.out.println("Second Smallest: "+arr[1]); System.out.println("Second Largest: "+arr[count-2]); } }
Output
Array = [55, 10, 8, 90, 43, 87, 95, 25, 50, 12] Smallest: 8 Largest: 95 Second Smallest: 10 Second Largest: 90
- Related Questions & Answers
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- C program to find the second largest and smallest numbers in an array
- Find the smallest and second smallest elements in an array in C++
- Maximum sum of smallest and second smallest in an array in C++ Program
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Maximum sum of smallest and second smallest in an array in C++
- Python program to find the second largest number in a list
- Program to find second largest digit in a string using Python
- Difference between the largest and the smallest primes in an array in Java
- Program to find Smallest and Largest Word in a String in C++
- How to find the second largest element in a user-input JavaScript array?
- Finding second smallest word in a string - JavaScript
- How to Find The Largest Or Smallest Items in Python?
- C++ Program to Find Second Smallest of n Elements with Given Complexity Constraint
Advertisements