Find all duplicate and missing numbers in given permutation array of 1 to N | Function to find the duplicate and the missing elements over the range [ 1 , N ] ; Stores the missing and duplicate numbers in the array arr [ ] ; Traverse the given array arr [ ] ; Check if the current element is not same as the element at index arr [ i ] - 1 , then swap ; Otherwise , increment the index ; Traverse the array again ; If the element is not at its correct position ; Stores the missing and the duplicate elements ; Print the Missing Number ; Print the Duplicate Number ; Driver code
function findElements ( arr , N ) { let i = 0 ; let missing = [ ] ; let duplicate = new Set ( ) ; while ( i != N ) { if ( arr [ i ] != arr [ arr [ i ] - 1 ] ) { t = arr [ i ] ; arr [ i ] = arr [ arr [ i ] - 1 ] ; arr [ t - 1 ] = t ; } else { i += 1 ; } } for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != i + 1 ) { missing . push ( i + 1 ) ; duplicate . add ( arr [ i ] ) ; } } document . write ( " " ) ; for ( it of missing ) document . write ( it + " " ) ; document . write ( " " ) ; for ( let it of [ ... duplicate ] ) document . write ( it + " " ) ; } let arr = [ 1 , 2 , 2 , 2 , 4 , 5 , 7 ] ; let N = arr . length ; findElements ( arr , N ) ;