# Programming Videos

## Search

### Java Program to Find Second Smallest Number in an Array

Write a java program to find second smallest number in an array without sorting. Given an unsorted array of integers, we have to write a code to find second smallest number without sorting an array.

If we use sorting then the time complexity of an algorithm is O(nlogn). You can read about various sorting algorithms and their time complexity.

We can solve this problem in a single traversal in O(n) time complexity.

C program to find second smallest number in an array without sorting

## Java Program to Find Second Smallest Number in an Array

Let's write a java code to find second smallest number in an array in a single traversal. The time complexity of this approach is O(n).

```import java.util.Scanner;

public class secondSmallest {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

//Input size of an array
System.out.println("Enter the size of an array");
int n = in.nextInt();

//Array declaration
int arr[] = new int[n];

System.out.println("Enter an array elements");

//Taking an input value of an array
for (int j = 0; j < arr.length; j++) {
arr[j] = in.nextInt();
}

//Initialize with max value of integer
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;

//Traverse an array
for (int i = 0; i < arr.length; i++) {

if (smallest > arr[i]) {
secondSmallest = smallest;
smallest = arr[i];

}

if (arr[i] > smallest && arr[i] < secondSmallest) {
secondSmallest = arr[i];
}
}

System.out.println("Second smallest number is " + secondSmallest);
}

}

```

Output -

Java program to reverse a string using stack

Java program to find first non-repeating character in a string

Java program to print factorial of a number

Java program to find sum of n natural numbers using recursion