Find Smallest and Second Smallest Element in an Array in Java
Second Smallest Element in an array using java. The java program below sorts and finds the second Smallest number in an array. First it uses an array initialised to some random values. The code to accept, sort and find the second Smallest elements in an array using java is also given below.
Smallest element in an array in java using for loop.
Java Program to Find Second Smallest Element in an Array
import java.util.Arrays;
public class SecondSmallestElement{
public static void main(String[] args) {
// initialized an array you can also accept one
int[] numbersArray = {60,20,30,40,50};
// sorted the array in ascending order
// using static method sort() of java.util.Arrays class
Arrays.sort(numbersArray);
System.out.println("Second smallest element in an Array is " + numbersArray[1]);
}
}
/* Output of above code:-
Second smallest element in an Array is 30
*/
Find the second Smallest element in an array using java. Accept the array from user using for loop.import java.io.*;
import java.util.Arrays;
public class SecondSmallestElement{
public static void main(String[] args) throws IOException {
int size;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many elements to accept in an array: ");
size = Integer.parseInt(br.readLine());
int[] numbersArray = new int[size];
//accept the array elements
for(int i=0;i<size;i++)
{
System.out.println("Enter " + (i+1) + "th element of array:" );
numbersArray[i] = Integer.parseInt(br.readLine());
}
// sorted the array in ascending order
// using static method sort() of java.util.Arrays class
Arrays.sort(numbersArray);
System.out.println("Second smallest element in an Array is " + numbersArray[1]);
}
}
/* Output of above code:-
How many elements to accept in an array: 5
Enter 1th element of array: 20
Enter 2th element of array: 10
Enter 3th element of array: 50
Enter 4th element of array: 90
Enter 5th element of array: 101
Smallest element in an Array is 10
Second smallest element in an Array is 20
*/
Smallest element in an array in java using for loop.
import java.io.*;
import java.util.Arrays;
public class SecondSmallestElement{
public static void main(String[] args) throws IOException {
int size;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many elements to accept in an array: ");
size = Integer.parseInt(br.readLine());
int[] numbersArray = new int[size];
//accept the array elements
for(int i=0;i<size;i++)
{
System.out.println("Enter " + (i+1) + "th element of array:" );
numbersArray[i] = Integer.parseInt(br.readLine());
}
// finding smallest number in an array using
// for loop
int smallest = numbersArray[0];
for(int i=0;i<size;i++)
{
if(numbersArray[i]<smallest)
smallest = numbersArray[i];
}
System.out.println("Smallest element in an Array is " + smallest);
}
}
/* Output of above code:-
How many elements to accept in an array: 5
Enter 1th element of array: 20
Enter 2th element of array: 10
Enter 3th element of array: 40
Enter 4th element of array: 90
Enter 5th element of array: 101
Smallest element in an Array is 10
*/