Count distinct elements in every window of size k | Counts distinct elements in window of size k ; Traverse the window ; Check if element arr [ i ] exists in arr [ 0. . i - 1 ] ; Counts distinct elements in all windows of size k ; Traverse through every window ; Driver program
function countWindowDistinct ( win , k ) { let dist_count = 0 ; for ( let i = 0 ; i < k ; i ++ ) { let j ; for ( j = 0 ; j < i ; j ++ ) if ( win [ i ] == win [ j ] ) break ; if ( j == i ) dist_count ++ ; } return dist_count ; } function countDistinct ( arr , n , k ) { for ( let i = 0 ; i <= n - k ; i ++ ) document . write ( countWindowDistinct ( arr . slice ( i , arr . length ) , k ) + " " ) ; } let arr = [ 1 , 2 , 1 , 3 , 4 , 2 , 3 ] , k = 4 ; countDistinct ( arr , arr . length , k ) ;