How To Find The Second Smallest Element In An Array In C++

In programming, an array is a data structure that contains a collection of elements of the same data type. These items are kept in contiguous memory locations, which means they are sequentially stored in memory.

Arrays are commonly used when dealing with a group of comparable elements, such as a list of integers, letters, or objects. Each element in the array is accessed using its index, which represents its location inside the array. In most programming languages, array indices begin with 0, so the first member is at index 0, the second at index 1, etc.

Example 1:

Array: {4, 2, 1, 5, 3}

  • The smallest element is 1 while the second-smallest is 2.
  • The function findSecondSmallest is called with this array, and it correctly finds 2 as the second smallest element.
  • Output: "Example 1: The second smallest element in the array is: 2"
  • Example 2:

Array: {9, 7, 6, 4, 10}

  • The lowest element is 4, while the second smallest is 6.
  • The function findSecondSmallest is called with this array, and it properly finds 6 as the second smallest element.
  • Output: "Example 2: The second smallest element in an array is: 6"
  • Pseudocode:

    Example
    
    Function findSecondSmallest(array)
        smallest = Infinity
        secondSmallest = Infinity
        For each element in the array
            If element < smallest
                secondSmallest = smallest
                smallest = element
            Else if element < secondSmallest and element ≠ smallest
                secondSmallest = element
        Return secondSmallest
    

This pseudocode describes the steps required to identify the second smallest entry in an array:

  • First, initialize variables smallest and secondSmallest to positive infinity (or the highest value possible for your data type).
  • Iterate through every element of the array to check for the elements.
  • If the current element is less than the smallest, update both the smallest and secondSmallest.
  • If the current element is less than secondSmallest but not equal to the smallest, update secondSmallest.
  • After iterating over all components, return secondSmallest.
  • Example 1:

Let us take an example to illustrate how to find the second smallest element in array in C++ .

Example

#include <iostream>
#include <limits>
int findSecondSmallest(int arr[], int size) {
    int smallest = std::numeric_limits<int>::max(); // Initialize to maximum possible value
    int secondSmallest = std::numeric_limits<int>::max(); // Initialize to maximum possible value
    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() {
    int arr[] = {4, 2, 1, 5, 3}; // Example array
    int size = sizeof(arr) / sizeof(arr[0]);

    int secondSmallest = findSecondSmallest(arr, size);
    std::cout << "The second smallest element in the array is: " << secondSmallest << std::endl;
    return 0;
}

Output:

Example 2:

Let us take another example to illustrate how to find the second smallest element in array in C++ .

Example

#include <iostream>
#include <limits>
int findScndsml(int a[], int s) {
    int smlest = std::numeric_limits<int>::max(); // Initialize to maximum possible value
    int scndsmlst = std::numeric_limits<int>::max(); // Initialize to maximum possible value
    for (int i = 0; i < s; ++i) {
        if (a[i] < smlest) {
            scndsmlst = smlest;
            smlest = a[i];
        } else if (a[i] < scndsmlst && a[i] != smlest) {
            scndsmlst = a[i];
        }
    }
    return scndsmlst;
}
int main() {
    int a1[] = {1, 2, 5, 4, 3}; // Example array 1
    int s1 = sizeof(a1) / sizeof(a1[0]);
    int scndsmlest1= findScndsml(a1, s1);
    std::cout << "Example 1: The second smallest element in the array is: " << scndsmlest1 << std::endl;
    int a2[] = {6, 7, 9, 5, 4}; // Example array 2
    int s2 = sizeof(a2) / sizeof(a2[0]);
    int scndsmlest2= findScndsml(a2, s2);
    std::cout << "Example 2: The second smallest element in the array is: " << scndsmlest2 << std::endl;
    return 0;
}

Output:

Example 3:

Example

// C++ program to find the smallest and second smallest elements
#include <bits/stdc++.h>
using namespace std; /* For INT_MAX */
void print2S(int a[], int a_size)
{
	int i, f, s;
	/* There should be atleast two elements */
	if (a_size < 2) {
		cout << " Invalid Input ";
		return;
	}
	f = s = INT_MAX;
	for (i = 0; i < a_size; i++) {
		/* If the present element is smaller than the first then update both the first and second elements in an array */
		if (a[i] < f) {
			s = f;
			f = a[i];
		}
		/* If a[i] is in between the first and second elements of an array then update the second element */
		else if (a[i] < s && a[i] != f)
			s = a[i];
	}
	if (s == INT_MAX)
		cout << "There is no second smallest element\n";
	else
		cout << "The smallest element is " << f
			<< " and second "
				"Smallest element is "
			<< s << endl;
}
/* Driver code */
int main()
{
	int a[] = { 111, 13, 25, 9, 34, 1 };
	int n = sizeof(a) / sizeof(a[0]);
	print2S(a, n);
	return 0;
}

Output:

Input Required

This code uses input(). Please provide values below: