human
Check if a given array contains duplicate elements within k distance from each other | Python 3 program to Check if a given array contains duplicate elements within k distance from each other.
Creates an empty list.
Traverse the input array.
If already present n hash , then we found a duplicate within k distance.
Add this item to hashset.
Remove the k + 1 distant item.
Driver Code"
Solution in Python:
 Write me a code to check if a given array contains duplicate elements within k distance from each other and the other steps required?
assistant
#Here is code to check if a given array contains duplicate elements within k distance from each other and the other steps required.
 def checkDuplicatesWithinK ( arr , n , k ) :
	myset = [ ]
	for i in range ( n ) :
		if arr [ i ] in myset :
			return True
		myset . append ( arr [ i ] )
		if ( i >= k ) :
			myset . remove ( arr [ i - k ] )
	return False
if __name__ == " _ _ main _ _ " :
	arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ]
	n = len ( arr )
	if ( checkDuplicatesWithinK ( arr , n , 3 ) ) :
		print ( " Yes " )
	else :
		print ( " No " )