Print elements of an array according to the order defined by another array | set 2 | Function to print an array according to the order defined by another array ; Declaring map and iterator ; Store the frequency of each number of a1 [ ] int the map ; Traverse through a2 [ ] ; Check whether number is present in map or not ; Print that number that many times of its frequency ; Print those numbers that are not present in a2 [ ] ; Driver code
def print_in_order ( a1 , a2 , n , m ) : NEW_LINE INDENT mp = dict . fromkeys ( a1 , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ a1 [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT if a2 [ i ] in mp . keys ( ) : NEW_LINE INDENT for j in range ( mp [ a2 [ i ] ] ) : NEW_LINE INDENT print ( a2 [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT del ( mp [ a2 [ i ] ] ) ; NEW_LINE DEDENT DEDENT for key , value in mp . items ( ) : NEW_LINE INDENT for j in range ( value ) : NEW_LINE INDENT print ( key , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT print ( ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a1 = [ 2 , 1 , 2 , 5 , 7 , 1 , 9 , 3 , 6 , 8 , 8 ] ; NEW_LINE a2 = [ 2 , 1 , 8 , 3 ] ; NEW_LINE n = len ( a1 ) ; NEW_LINE m = len ( a2 ) ; NEW_LINE print_in_order ( a1 , a2 , n , m ) ; NEW_LINE DEDENT