Write constraint for an integer array with 10 elements such that exactly 3 of them are same and rest are unique

In reply to dve12:

This is one of the compact way I could think of

class generate_array#(parameter repeat_n = 3, parameter arr_size = 10, parameter elem_dim = 4);
   rand bit [elem_dim-1:0] a[arr_size];
   rand bit [elem_dim-1:0] b;
   
   // unique and repeat 3 times speciefied by the param
   constraint unique_and_repeat {
       b inside {a};
       foreach(a[i])
            a.sum() with (8'(item == a[i])) == ((a[i]%b == 0) & (a[i]/b ==1) ? repeat_n:1);
   }
   
   function void post_randomize();
       a.sort() ;  
   endfunction 
endclass
 
module top_tb;
   // PARAM
   parameter repeat_n = 3; 
   parameter arr_size = 10;
   parameter elem_dim = 4; 

   generate_array#(repeat_n,arr_size,elem_dim) my_arr;
 
   initial begin   
      my_arr = new();  
      repeat(arr_size) begin
        if(my_arr.randomize()) begin       
            $display(" b is %0h " , my_arr.b);
            $display(" a is %0p " , my_arr.a);       
        end  
      else 
          $display("Fail in randomization");
      end
   end  
endmodule