I have an array of 10 elements in which some elements are repeated . Need to get the repeated values and how many times it is repeated and the first index of the repeated value.
A brute force solution that I can think of is this -
class packet;
int count[11];
int first_index[11];
function void count_and_index(int a[], output int count[11], output int first_index[11]);
for(int i=0; i < a.size(); i++) begin
for(int j=i+1; j< a.size(); j++) begin
if( a[i] == a[j]) begin
$display("value of a[i] and a[j] are %d, and %d and i and j are %d, %d", a[i], a[j], i, j);
count[a[i]] = count[a[i]] + 1;
if(count[a[i]] == 1) first_index[a[i]] = j;
end
end
end
endfunction
endclass
module tb;
initial begin
int a[11] = '{0,1,2,3,4,2,3,1,5,6,7};
int final_count[11], final_first_index[11];
packet p = new();
p.count_and_index(a, final_count, final_first_index);
$display("final_count = %p", final_count);
$display("final_first_index = %p", final_first_index);
end
endmodule
I am not getting what is the significance of count[11], first_index[11] of the function used for …
Are they checking how many times they are repeated and the index’s ?