Program to check :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.
More Description
Program Logic
- Initialization:
- We start by initializing two variables
smallest
andsecondSmallest
with the maximum possible integer value. This ensures that any element in the array will be smaller than these initial values.
- We start by initializing two variables
- Iteration through the array:
- We iterate through each element of the array.
- Comparison with
smallest
andsecondSmallest
:- During each iteration, we compare the current element with
smallest
andsecondSmallest
.
- During each iteration, we compare the current element with
- Updating
smallest
andsecondSmallest
:- If the current element is smaller than
smallest
, it means we’ve found a new smallest element. So, we updatesecondSmallest
tosmallest
andsmallest
to the current element. - If the current element is greater than
smallest
but smaller thansecondSmallest
, it means we’ve found a new second smallest element. So, we updatesecondSmallest
to the current element. - We also ensure that the current element is not equal to
smallest
while updatingsecondSmallest
. This is to handle cases where there might be duplicate smallest elements in the array.
- If the current element is smaller than
- Returning
secondSmallest
:- After iterating through all elements, the value of
secondSmallest
will represent the second smallest element in the array. So, we returnsecondSmallest
.
- After iterating through all elements, the value of
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