Write a program to find the second smallest element in a given array of integers.
Write a program to find the second smallest element in a given array of integers.
Student
I am Utpal Vishwas from Uttar Pradesh. Have completed my B. Tech. course from MNNIT campus Prayagraj in 2022. I have good knowledge of computer networking.
In this program, we first ask the user to input the number of elements in the array and then we take the input of the elements. We then sort the array in ascending order using a simple bubble sort algorithm. Finally, we find the second smallest element by looping through the array and checking for the element which is not equal to the first element of the array (which is the smallest element).
Note: This program assumes that there are at least two distinct elements in the array. If all elements are the same, it will output an incorrect result.
Given below is a sample program to find the second smallest number in an array.