Initialize a variable to count the number of elements it is a factor of
int count = 0 ; for ( int j = 0 ; j < arr . Length ; j ++ ) if ( arr [ j ] % i == 0 ) count += 1 ; rank [ g ] = count ; factors [ g ] = i ; g ++ ; }