How to delete duplicate elements from associative array and Queue in System Verilog

In reply to chris_le:

What’s that fixed value 500? How do you know the duplicated item? How about more than 1 duplicated item = =a

For the queue, way too simple.
For the associative array, I have a way to detect duplicated element, but the question is which key we should keep? it’s not clear to me. check my code.


module automatic test;

  // access queue
  function void find_unique_from_q;
    int q_int[$];
    q_int = {500,1000,500,200,400,500,600,700,900};
    q_int= q_int.unique();
  endfunction
  
  // access hash
  function void find_unique_from_hash;
    int age[string];
    int element[$];
    int ret[$];
    age["bob"] = 32;
    age["timmy"] = 4;
    age["tyrian"] = 31;
    age["sara"] = 2; 
    age["beema"] = 4;
    age["jack"]=31;
    
    element = age.find with(1);
    ret = element.unique(); 
    // use ret to update age hash_table
    // but you didn't mention which key we should keep, (beema or timmy?)
  endfunction

initial begin
 find_unique_from_hash();
 find_unique_from_q();
end

endmodule