## The challenge

Given an array/list [] of integers, Find the Nth smallest element in this array of integers

Notes

• Array/list size is at least 3 .
• Array/list’s numbers could be a mixture of positives , negatives and zeros .
• Repetition in array/list’s numbers could occur, so don’t Remove Duplications .

Examples

 ``````1 2 3 `````` ``````nthSmallest({3,1,2} ,2) ==> return (2) nthSmallest({15,20,7,10,4,3} ,3) ==> return (7) nthSmallest({2,169,13,-5,0,-1} ,4) ==> return (2) ``````

## The solution in Java code

Option 1:

 ``````1 2 3 4 5 6 7 8 9 `````` ``````public class Solution { public static int nthSmallest(final int[] arr, final int n) { java.util.Arrays.sort(arr); for(int i=0; i

Option 2:

 ``````1 2 3 4 5 6 7 `````` ``````import java.util.Arrays; public class Solution { public static int nthSmallest(final int[] arr, final int n) { Arrays.sort(arr); return arr[n - 1]; } } ``````

Option 3:

 ``````1 2 3 4 5 6 7 `````` ``````import java.util.Arrays; public class Solution { public static int nthSmallest(final int[] arr, final int n) { return Arrays.stream(arr).sorted().skip(n-1).findFirst().getAsInt(); } } ``````

## Test cases to validate our solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 `````` ``````import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SampleTest { @Test public void checkPositiveValues() { doTest(new int[]{3, 1, 2}, 2, 2); doTest(new int[]{15, 20, 7, 10, 4, 3}, 3, 7); } @Test public void checkNegativeValues() { doTest(new int[]{-5, -1, -6, -18}, 4, -1); doTest(new int[]{-102, -16, -1, -2, -367, -9}, 5, -2); } @Test public void checkMixedValues() { doTest(new int[]{2, 169, 13, -5, 0, -1}, 4, 2); doTest(new int[]{177, 225, 243, -169, -12, -5, 2, 92}, 5, 92); } private void doTest(final int[] arr, final int n, final int expected) { assertEquals(expected, Solution.nthSmallest(arr, n)); } } ``````