How to Find the Second Smallest Element in an Array in C++? Last Updated : 18 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element in the array is: 3Finding the Second Smallest Element in an Array in C++ To find the second smallest element in the array, we will have to iterate the array and keep the track of current smallest and second smallest values. The below approach shows how to do it. AlgorithmInitialize two variables smallest and secondSmallest first array element.Traverse through each element of the array.For each element in the array: If the element is smaller than smallest update secondSmallest with the current value of smallest and update smallest with the current array value. If the element is not smaller than the smallest but smaller than secondSmallest then update the value of secondSmallest.Return secondSmallest which holds the value of the second smallest element in the array.C++ Program to Find the Second Smallest Element in an ArrayThe following program illustrates how we can find the second smallest element in an array in C++. C++ // C++ Program to how to find the second smallest element in // an array #include <climits> #include <iostream> using namespace std; // function to find the second smallest element int findSecondSmallest(int arr[], int size) { if (size < 2) { cout << "Array should have at least two elements." << endl; return INT_MAX; } int smallest = INT_MAX; int secondSmallest = INT_MAX; for (int i = 0; i < size; ++i) { if (arr[i] < smallest) { secondSmallest = smallest; smallest = arr[i]; } else if (arr[i] < secondSmallest && arr[i] != smallest) { secondSmallest = arr[i]; } } return secondSmallest; } int main() { // Intializing an array int arr[] = { 10, 5, 8, 2, 7, 3, 15 }; // Finding the size of the array int size = sizeof(arr) / sizeof(arr[0]); // Finding the second smallest element of the array int res = findSecondSmallest(arr, size); // Printing the second smallest element of the array if (res != INT_MAX) { cout << "The second smallest element in the array " "is: " << res << endl; } return 0; } OutputThe second smallest element in the array is: 3 Time Complexity: O(N), where N is the number of array elements.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Index of an Element in an Array in C++? A anjalibo6rb0 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read How to Find the Index of an Element in an Array in C++? Given an array of n elements, the task is to find the index of a specific element in C++.ExamplesInput: arr[] = {11, 13, 9, 21, 51, 1000}, val = 9Output: 2Explanation: As the value 9 is present at index 2.Input: arr[] = {5, 8, 12, 9, 11, 32}, val = 11Output: 4Explanation: As the value 11 is present 3 min read How to Find the Third Smallest Number in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the third smallest element in an array in C++. If there is no third smallest elemen 2 min read How to Find All Indexes of an Element in an Array in C++? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to find all indexes of a specific element in an array in C++. Example: Input: myArray = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4}; target = 3Output: The element 3 occurred at 2 min read How to Find the First Element in a Set in C++? In C++, a set is a container that stores unique elements following a specific order. In this article, we will learn how to find the first element in a set. Example: Input: mySet = {1, 2, 4, 3, 8, 4, 7, 8, 6, 4} Output: First Element = 1Find the First Element in a Set in C++To find the first element 1 min read How to Find the Minimum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that stores data in non-contiguous memory locations efficiently. In this article, we will learn how to find the minimum element in a list in C++. Example: Input: myList = {30, 10, 20, 50, 40};Output: The minimum element in the list i 2 min read Like