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 ?
It would help to get all that information upfront along with how you want the results reported.
Here’s an example piece of code that gives you the information you requested, but probably not in the format you wanted it to because we don’t know what that is.
module tb;
int a[], repeats[];
int count[int] = '{default:0};
int firsts[int][$];
initial begin
a = {0,1,2,3,4,2,3,1,5,6, 7, 7};
foreach (a[i]) count[a[i]]++; // gets counts of all array values
repeats = count.find_index() with (item>1); // gets all counts >1
foreach(repeats[i])
firsts[repeats[i]] = a.find_first_index() with (item == repeats[i]);
$display("%p",repeats);
$display("%p",firsts);
end
endmodule