Second Smallest Element in an Array

Definition & Explanation

The statement “find the second smallest element in the array” means that given an array of integers, you need to find the element that is smallest in value among all elements except the smallest element itself.

Program Logic

  1. Initialization:
    • We start by initializing two variables smallest and secondSmallest with the maximum possible integer value. This ensures that any element in the array will be smaller than these initial values.
  2. Iteration through the array:
    • We iterate through each element of the array.
  3. Comparison with smallest and secondSmallest:
    • During each iteration, we compare the current element with smallest and secondSmallest.
  4. Updating smallest and secondSmallest:
    • If the current element is smaller than smallest, it means we’ve found a new smallest element. So, we update secondSmallest to smallest and smallest to the current element.
    • If the current element is greater than smallest but smaller than secondSmallest, it means we’ve found a new second smallest element. So, we update secondSmallest to the current element.
    • We also ensure that the current element is not equal to smallest while updating secondSmallest. This is to handle cases where there might be duplicate smallest elements in the array.
  5. Returning secondSmallest:
    • After iterating through all elements, the value of secondSmallest will represent the second smallest element in the array. So, we return secondSmallest.

By following this logic, we effectively identify the second smallest element in the array. This approach ensures that we only iterate through the array once, making it an efficient solution for finding the second smallest element.

C

Method 1 :

#include <stdio.h>
#include <limits.h>

int findSecondSmallest(int arr[], int size) {
    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() {
    int arr[] = {5, 3, 8, 1, 9};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    int secondSmallest = findSecondSmallest(arr, size);
    printf("The second smallest element in the array is: %d\n", secondSmallest);
    
    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
#include <climits>
using namespace std;

int findSecondSmallest(int arr[], int size) {
    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() {
    int arr[] = {5, 3, 8, 1, 9};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    int secondSmallest = findSecondSmallest(arr, size);
    cout << "The second smallest element in the array is: " << secondSmallest << endl;
    
    return 0;
}

Output :

JAVA

Method 1 :

public class Main {
    public static int findSecondSmallest(int[] arr) {
        int smallest = Integer.MAX_VALUE;
        int secondSmallest = Integer.MAX_VALUE;
        
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < smallest) {
                secondSmallest = smallest;
                smallest = arr[i];
            } else if (arr[i] < secondSmallest && arr[i] != smallest) {
                secondSmallest = arr[i];
            }
        }
        return secondSmallest;
    }

    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 1, 9};
        
        int secondSmallest = findSecondSmallest(arr);
        System.out.println("The second smallest element in the array is: " + secondSmallest);
    }
}

Output :

Python

Method 1 :

import math
arr=[10,13,17,11,34,21]
first=math.inf
second=math.inf

for i in range(0,len(arr)):

    if arr[i]>first:
        first=arr[i]

for i in range(0,len(arr)):
    if arr[i]!= first and arr[i]<second:
        second=arr[i]


print(second)

Output :

10