- Published on

# Find smallest positive integer from array in Javascript

- Authors
- Name
- Dhaval Vyas
- @VyasDhaval_

This article is about finding the smallest positive integer from an array.

## Option 1

```
const arr = [1,4,53,9,10,43];
const sorted = arr.sort((a,b) => (a-b));
retrun sorted[0]; // 1
```

Option 1 looks clean but the time complexity of the above solution is O(n log(n)). There is a better way to achieve this. Check out option 2.

## Option 2

```
const arr = [1, 4, 53, 9, 10, 43]
function findSmallest(_arr) {
let smallest = _arr[0]
for (let i = 0; i < _arr.length; i++) {
if (_arr[i] < smallest) {
smallest = _arr[i]
}
}
return smallest
}
findSmallest(arr) // 1
```

Performance of Option 2 is better than Option 1. You can create this as a utility function in your project. Checkout this benchmark here.