











C++ program for finding the second smallest element in an array
Finding second smallest element in array
An array is a group of similar type of data that are addressed by an common name (Arr[]). Array of any type can be created and may have one or more dimensions, i.e. 1-D Array, 2-D Array etc. Here we will learn how to code a C++ program for finding second smallest element in an array.


Algorithms:-
- Take array as input
- sort the array in ascending order using any sorting algorithm
- traverse through the array and compare wether the element is equal to first element of array
- if element is not equal to first element return the element
- if no second element found return -1
C++ Code:
//C++ program
//program for finding the second smallest element in an array
#include<iostream>
using namespace std;
int prep(int a[], int total)
{
int temp;
for (int i = 0; i < total–1; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i=1;i<total;i++)
{
if(a[i]!=a[0])
return a[i];
}
return –1 ; //all elements are equal
}
int main()
{
int a[]={7,8,9,5,2};
cout<<“Second smallest: “<<prep(a,5));
return 0;
}
Output–
Second smallest: 5
Important Codes related to Arrays
- Find Smallest Element in an Array : C | C++ | Java | Python
- Find Second Smallest Element in an Array : C | C++ | Java | Python
- Find Largest element in an array : C | C++ | Java | Python
- Find the Smallest and largest element in an array : C | C++ | Java | Python
- Calculate the sum of elements in an array : C | C++ | Java | Python
- Reverse an Array : C | C++ | Java | Python
- Sort first half in ascending order and second half in descending : C | C++ | Java | Python
- Sort the elements of an array : C | C++ | Java | Python
- Finding the frequency of elements in an array : C | C++ | Java | Python
- Finding the Longest Palindrome in an Array : C | C++ | Java| Python
- Counting Distinct Elements in an Array : C | C++ | Java| Python
- Finding Repeating elements in an Array : C | C++ | Java | Python
- Finding Non Repeating elements in an Array : C | C++ | Java | Python
- Removing Duplicate elements from an array : C | C++ | Java
- Finding Minimum scalar product of two vectors : C | C++ | Java | Python
- Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
- Counting the number of even and odd elements in an array : C | C++ | Java
- Find all Symmetric pairs in an array : C | C++ | Java
- Find maximum product sub-array in a given array : C | C++ | Java
- Finding Arrays are disjoint or not : C | C++ | Java
- Determine Array is a subset of another array or not : C | C++ | Java
- Determine can all numbers of an array be made equal : C | C++ | Java
- Finding Minimum sum of absolute difference of given array : C | C++ | Java
- Sorting elements of an array by frequency : C | C++ | Java
- Sort an array according to the order defined by another array : C | C++ | Java
- Replace each element of the array by its rank in the array : C | C++ | Java
- Finding equilibrium index of an array : C | C++ | Java| Python
- Rotation of elements of array- left and right : C | C++ | Java| Python
- Block swap algorithm for array rotation : C | C++ | Java| Python
- Juggling algorithm for array rotation : C | C++ | Java | Python
- Finding Circular rotation of an array by K positions : C | C++ | Java | Python
You can find the below Programs in all languages
- Program for Pyramid star pattern
- Program for Pyramid number pattern
- Program for Palindromic Pyramid Pattern – Java | Python
- Program for Diamond star pattern
- Program for Diamond number pattern
- Program for Floyd’s Trianlge – Java | Python
- Program for Pascal triangle – Java | Python
- Program to Check array similarity
- Program for Square sum
- Program for Longest palindrome in the array


Login/Signup to comment