Home >> Programming >> Find Second Smallest Element in the Array Hey there, welcome back to another post. In this post, we will learn how to find the second smallest number in the array in Java. Find Second Smallest Element in the Array Problem Statement You have given an integer array, return the second smallest element from the array if exists else return no second smallest element exists. Example 1: Input: [1, 4, 2, 7, 90, -1, -4] Output: -1 Find Second Smallest Element in the Array Solution using Single Iteration Approach By using the below steps we can find the second smallest element in the array by using a single iteration: Initialize two variables first and second for smallest and second smallest numbers and the initial value will be Integer.MAX_VALUE. Traverse the array and check if the current element is smaller than the smallest element then update both first and second. If the current element is between first and second then only update...
Comments
Post a Comment