Skip to main content

Write a java program to find the second smallest element in an array.

· One min read
Kaustubh Kulkarni
SecondSmallestInArray.java
public class SecondSmallestInArray{    
public static int getSecondSmallest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[1];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
System.out.println("Second smallest: "+getSecondSmallest(a,6));
}}


OutPut:

  
$ java second_smallest_element_in_array.java
Second smallest: 2
Second smallest: 33