Count distinct elements in an array | C # program to count distinct elements in a given array ; Pick all elements one by one ; If not printed earlier , then print it ; Driver code
using System ; class GFG { static int countDistinct ( int [ ] arr , int n ) { int res = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int j = 0 ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) res ++ ; } return res ; } public static void Main ( ) { int [ ] arr = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 } ; int n = arr . Length ; Console . WriteLine ( countDistinct ( arr , n ) ) ; } }