Find the smallest positive number missing from an unsorted array | Set 1 | Function for finding the first missing positive number ; Check if 1 is present in array or not ; Loop to check boundary condition and for swapping ; Finding which index has value less than n ; If array has values from 1 to n ; Driver Code
function firstMissingPositive ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { while ( arr [ i ] >= 1 && arr [ i ] <= n && arr [ i ] != arr [ arr [ i ] - 1 ] ) { let temp = arr [ arr [ i ] - 1 ] ; arr [ arr [ i ] - 1 ] = arr [ i ] ; arr [ i ] = temp ; } } for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] != i + 1 ) return ( i + 1 ) ; return ( n + 1 ) ; } let arr = [ 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 ] ; let n = arr . length ; let ans = firstMissingPositive ( arr , n ) ; document . write ( ans ) ;