human
Can you provide a code to count distinct elements in an array and the other steps required given the following:
Count distinct elements in an array | Python3 program to count all distinct elements in a given array.
First sort the array so that all occurrences become consecutive.
Traverse the sorted array.
Move the index ahead while there are duplicates.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to count distinct elements in an array and the other steps required.
 def countDistinct ( arr , n ) :
	arr . sort ( ) ;
	res = 0 ;
	i = 0 ;
	while ( i < n ) :
		while ( i < n - 1 and arr [ i ] == arr [ i + 1 ] ) :
			i += 1 ;
		res += 1 ;
		i += 1 ;
	return res ;
arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] ;
n = len ( arr ) ;
print ( countDistinct ( arr , n ) ) ;