# How to Find the Smallest Integer in the Array in Java

## The challenge

Given an array of integers your solution should find the smallest integer.

For example:

• Given `[34, 15, 88, 2]` your solution will return `2`
• Given `[34, -345, -1, 100]` your solution will return `-345`

You can assume, for the purpose of this challenge, that the supplied array will not be empty.

## The solution in Java

Option 1 (using `Arrays.sort`):

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```import java.util.Arrays;

public class SmallestIntegerFinder {
public static int findSmallestInt(int[] args) {

Arrays.sort(args);

return args[0];
}
}
```Code language: Java (java)```

Option 2 (using `streams`):

``````import java.util.stream.IntStream;

public class SmallestIntegerFinder {
public static int findSmallestInt(int[] args) {
return IntStream.of(args).min().getAsInt();
}
}
```Code language: Java (java)```

## Test cases to validate the solution

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SmallestIntegerFinderTest {

@Test
public void example1(){
int expected = 11;
int actual = SmallestIntegerFinder.findSmallestInt(new int[]{78,56,232,12,11,43});
assertEquals(expected, actual);
}

@Test
public void example2(){
int expected = -33;
int actual = SmallestIntegerFinder.findSmallestInt(new int[]{78,56,-2,12,8,-33});
assertEquals(expected, actual);
}

@Test
public void example3(){
int expected = Integer.MIN_VALUE;
int actual = SmallestIntegerFinder.findSmallestInt(new int[]{0,Integer.MIN_VALUE,Integer.MAX_VALUE});
assertEquals(expected, actual);
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of