Count distinct elements in an array | PHP program to count distinct elements in a given array ; Pick all elements one by one ; If not printed earlier , then print it ; Driver Code
< ? php function countDistinct ( & $ arr , $ n ) { $ res = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] == $ arr [ $ j ] ) break ; if ( $ i == $ j ) $ res ++ ; } return $ res ; } $ arr = array ( 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 ) ; $ n = count ( $ arr ) ; echo countDistinct ( $ arr , $ n ) ; ? >