Program to Find Second Smallest and Second Largest Element from an Array

Program to Find Second Smallest and Second Largest Element from an Array

  • Write a program to Find Second Smallest and Second Largest Element from an Array in C
  • Write a program to Find Second Smallest and Second Largest Element from an Array in C++
  • Write a program to Find Second Smallest and Second Largest Element from an Array in Python
  • Write a program to Find Second Smallest and Second Largest Element from an Array in PHP
  • Write a program to Find Second Smallest and Second Largest Element from an Array in Java
  • Write a program to Find Second Smallest and Second Largest Element from an Array in Java Script
  • Write a program to Find Second Smallest and Second Largest Element from an Array in C#

Explanation:

The following reasoning can be used to determine an array’s second-smallest and second-largest elements:

Steps:

  1. Input Validation:
    • Ensure the array has at least two distinct elements.
    • Handle empty arrays or arrays with only one unique value.
  2. Initialize Variables:
    • Set smallest and second_smallets to appropriate initial values (e.g., infinity for Python or a large number).
    • Set largest and second_largest to appropriate initial values (e.g., -infinity or a small number).
  3. Traverse the Array:
    • For each element in the array:
      • Update the smallest and second smallest values.
      • Update the largest and second largest values.
  4. Output:
    • After the traversal, return the second smallest and second largest values.

Program to Find Second Smallest and Second Largest Element from an Array

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

int main() {
    int arr[] = {10, 20, 4, 45, 99}; // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    int first_smallest = INT_MAX, second_smallest = INT_MAX;
    int first_largest = INT_MIN, second_largest = INT_MIN;

    for (int i = 0; i < size; i++) {
        if (arr[i] < first_smallest) {
            second_smallest = first_smallest;
            first_smallest = arr[i];
        } else if (arr[i] < second_smallest && arr[i] != first_smallest) {
            second_smallest = arr[i];
        }

        if (arr[i] > first_largest) {
            second_largest = first_largest;
            first_largest = arr[i];
        } else if (arr[i] > second_largest && arr[i] != first_largest) {
            second_largest = arr[i];
        }
    }

    printf("Second Smallest: %d\n", second_smallest);
    printf("Second Largest: %d\n", second_largest);

    return 0;
}

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

int main() {
    int arr[] = {10, 20, 4, 45, 99}; // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    int first_smallest = INT_MAX, second_smallest = INT_MAX;
    int first_largest = INT_MIN, second_largest = INT_MIN;

    for (int i = 0; i < size; i++) {
        if (arr[i] < first_smallest) {
            second_smallest = first_smallest;
            first_smallest = arr[i];
        } else if (arr[i] < second_smallest && arr[i] != first_smallest) {
            second_smallest = arr[i];
        }

        if (arr[i] > first_largest) {
            second_largest = first_largest;
            first_largest = arr[i];
        } else if (arr[i] > second_largest && arr[i] != first_largest) {
            second_largest = arr[i];
        }
    }

    cout << "Second Smallest: " << second_smallest << endl;
    cout << "Second Largest: " << second_largest << endl;

    return 0;
}

arr = [10, 20, 4, 45, 99]  # Example array

first_smallest, second_smallest = float('inf'), float('inf')
first_largest, second_largest = float('-inf'), float('-inf')

for num in arr:
    if num < first_smallest:
        second_smallest = first_smallest
        first_smallest = num
    elif num < second_smallest and num != first_smallest:
        second_smallest = num
    
    if num > first_largest:
        second_largest = first_largest
        first_largest = num
    elif num > second_largest and num != first_largest:
        second_largest = num

print(f"Second Smallest: {second_smallest}")
print(f"Second Largest: {second_largest}")

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><?php
$arr = array(10, 20, 4, 45, 99);  // Example array

$first_smallest = PHP_INT_MAX;
$second_smallest = PHP_INT_MAX;
$first_largest = PHP_INT_MIN;
$second_largest = PHP_INT_MIN;

foreach ($arr as $num) {
    if ($num < $first_smallest) {
        $second_smallest = $first_smallest;
        $first_smallest = $num;
    } elseif ($num < $second_smallest && $num != $first_smallest) {
        $second_smallest = $num;
    }

    if ($num > $first_largest) {
        $second_largest = $first_largest;
        $first_largest = $num;
    } elseif ($num > $second_largest && $num != $first_largest) {
        $second_largest = $num;
    }
}

echo "Second Smallest: $second_smallest\n";
echo "Second Largest: $second_largest\n";
?>

public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 20, 4, 45, 99};  // Example array
        int firstSmallest = Integer.MAX_VALUE, secondSmallest = Integer.MAX_VALUE;
        int firstLargest = Integer.MIN_VALUE, secondLargest = Integer.MIN_VALUE;

        for (int num : arr) {
            if (num < firstSmallest) {
                secondSmallest = firstSmallest;
                firstSmallest = num;
            } else if (num < secondSmallest && num != firstSmallest) {
                secondSmallest = num;
            }

            if (num > firstLargest) {
                secondLargest = firstLargest;
                firstLargest = num;
            } else if (num > secondLargest && num != firstLargest) {
                secondLargest = num;
            }
        }

        System.out.println("Second Smallest: " + secondSmallest);
        System.out.println("Second Largest: " + secondLargest);
    }
}

const arr = [10, 20, 4, 45, 99];  // Example array

let firstSmallest = Infinity, secondSmallest = Infinity;
let firstLargest = -Infinity, secondLargest = -Infinity;

arr.forEach(num => {
    if (num < firstSmallest) {
        secondSmallest = firstSmallest;
        firstSmallest = num;
    } else if (num < secondSmallest && num !== firstSmallest) {
        secondSmallest = num;
    }

    if (num > firstLargest) {
        secondLargest = firstLargest;
        firstLargest = num;
    } else if (num > secondLargest && num !== firstLargest) {
        secondLargest = num;
    }
});

console.log(`Second Smallest: ${secondSmallest}`);
console.log(`Second Largest: ${secondLargest}`);

using System;

class Program {
    static void Main() {
        int[] arr = {10, 20, 4, 45, 99};  // Example array
        int firstSmallest = int.MaxValue, secondSmallest = int.MaxValue;
        int firstLargest = int.MinValue, secondLargest = int.MinValue;

        foreach (int num in arr) {
            if (num < firstSmallest) {
                secondSmallest = firstSmallest;
                firstSmallest = num;
            } else if (num < secondSmallest && num != firstSmallest) {
                secondSmallest = num;
            }

            if (num > firstLargest) {
                secondLargest = firstLargest;
                firstLargest = num;
            } else if (num > secondLargest && num != firstLargest) {
                secondLargest = num;
            }
        }

        Console.WriteLine($"Second Smallest: {secondSmallest}");
        Console.WriteLine($"Second Largest: {secondLargest}");
    }
}

List of All Programs