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
def findElements ( arr , N ) : NEW_LINE INDENT i = 0 ; NEW_LINE missing = [ ] ; NEW_LINE duplicate = set ( ) ; NEW_LINE while ( i != N ) : NEW_LINE INDENT if ( arr [ i ] != arr [ arr [ i ] - 1 ] ) : NEW_LINE INDENT t = arr [ i ] NEW_LINE arr [ i ] = arr [ arr [ i ] - 1 ] NEW_LINE arr [ t - 1 ] = t NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] != i + 1 ) : NEW_LINE INDENT missing . append ( i + 1 ) ; NEW_LINE duplicate . add ( arr [ i ] ) ; NEW_LINE DEDENT DEDENT print ( " Missing ▁ Numbers : ▁ " , end = " " ) ; NEW_LINE for it in missing : NEW_LINE INDENT print ( it , end = " ▁ " ) ; NEW_LINE DEDENT print ( " Duplicate Numbers : " , end = " " ) ; NEW_LINE for it in list ( duplicate ) : NEW_LINE INDENT print ( it , end = ' ▁ ' ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 2 , 4 , 5 , 7 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE findElements ( arr , N ) ; NEW_LINE DEDENT