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:

You have made the constraints much more complex than they need to be, And technically, you are not allowed a random index into a random array variable.

module abc;
  class aa;
    rand byte unsigned val[10], val3;
 
    constraint s1 {
      foreach(val[i]) {
        val[i] < 10;
        // every value except val3 must appear only once
        val[i] != val3 -> val.sum() with (int'(item==val[i]))== 1;
      }
      // val3 must appear three times in the array
      val.sum() with (int'(item==val3)) == 3;
                      }
  endclass
                      
  aa a1 = new();

  initial repeat (10) begin
    a1.randomize();
    $display("%p %d", a1.val, a1.val3);
  end
endmodule