Check if all subarrays contains at least one unique element | Python3 program for the above approach ; Function to check if all subarrays of array have at least one unique element ; Stores frequency of subarray elements ; Generate all subarrays ; Insert first element in map ; Update frequency of current subarray in the HashMap ; Check if at least one element occurs once in current subarray ; If any subarray doesn 't  have unique element ; Clear map for next subarray ; Return Yes if all subarray having at least 1 unique element ; Driver Code ; Given array arr [ ] ; Function Call
from collections import defaultdict NEW_LINE def check ( arr , n ) : NEW_LINE INDENT hm = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hm [ arr [ i ] ] += 1 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT hm [ arr [ j ] ] += 1 NEW_LINE flag = False NEW_LINE for k in hm . values ( ) : NEW_LINE INDENT if ( k == 1 ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not flag ) : NEW_LINE return " No " NEW_LINE DEDENT hm . clear ( ) NEW_LINE DEDENT return " Yes " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( check ( arr , N ) ) NEW_LINE DEDENT