Write a Java program to Find the second smallest element in the queue


The Java code defines a class SecondSmallestElement with a method findSecondSmallest that finds the second smallest element in a given Queue of integers.

  • The findSecondSmallest method takes a Queue<Integer> named queue as input and returns an integer representing the second smallest element in the queue.
  • Before processing the queue, the method checks if the queue has at least two elements using queue.size() < 2. If the queue does not have enough elements (less than two), it throws a NoSuchElementException with the message "Queue does not have enough elements."
  • The method initializes two variables: smallest and secondSmallest to Integer.MAX_VALUE. These variables will be used to keep track of the smallest and second smallest elements in the queue.
  • The method uses a loop to iterate through each element in the queue using an enhanced for loop (also known as a "for-each" loop). In each iteration, the variable num takes the value of the current element from the queue.
  • Inside the loop, the code checks if num is smaller than the current smallest element. If num is smaller, it updates the value of secondSmallest to the previous value of smallest, and then updates the value of smallest to num.
  • If num is not smaller than smallest, the code checks if num is smaller than the current secondSmallest and also makes sure that num is not equal to smallest. This condition ensures that the second smallest element is not a duplicate of the smallest element.
  • If both conditions are true, it means num is the new second smallest element, and it updates the value of secondSmallest to num.
  • After the loop finishes, the variable secondSmallest will hold the second smallest element in the queue.
  • The method returns the value of secondSmallest.
  • In the main method, a Queue<Integer> named queue is created using the LinkedList implementation of the Queue interface. Integers are added to the queue using the offer() method.
  • The original queue is printed using System.out.println().
  • The findSecondSmallest method is then called with the queue as an argument, and the second smallest element returned by the method is stored in the variable secondSmallest.
  • Finally, the second smallest element in the queue is printed using System.out.println().

Source Code

import java.util.*;
 
public class SecondSmallestElement
{
	public static int findSecondSmallest(Queue<Integer> queue)
	{
		if (queue.size() < 2)
		{
			throw new NoSuchElementException("Queue does not have enough elements");
		}
 
		int smallest = Integer.MAX_VALUE;
		int secondSmallest = Integer.MAX_VALUE;
 
		for (int num : queue)
		{
			if (num < smallest)
			{
				secondSmallest = smallest;
				smallest = num;
			} else if (num < secondSmallest && num != smallest)
			{
				secondSmallest = num;
			}
		}
 
		return secondSmallest;
    }
 
	public static void main(String[] args)
	{
		Queue<Integer> queue = new LinkedList<>();
		queue.offer(1);
		queue.offer(2);
		queue.offer(3);
		queue.offer(4);
		queue.offer(5);
		System.out.println("Original Queue : "+queue);
 
		int secondSmallest = findSecondSmallest(queue);
		System.out.println("Second smallest element in the queue : " + secondSmallest);
	}
}
 

Output

Original Queue : [1, 2, 3, 4, 5]
Second smallest element in the queue : 2

Example Programs