Count distinct elements in an array | Java program to count distinct elements in a given array ; Pick all elements one by one ; If not printed earlier , then print it ; Driver code
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 ( String [ ] args ) { int arr [ ] = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 } ; int n = arr . length ; System . out . println ( countDistinct ( arr , n ) ) ; } }