def permute_unique_set(nums: list[int]) -> list[list[int]]:
    """Compute all the *unique* permutations of the elements in a given input array

    Args:
        nums: array of possibly non-distinct elements

    Returns: all *unique* permutations of elements in `nums`

    Examples:
        >>> sorted(permute_unique_set([1,1,2]))
        [[1, 1, 2], [1, 2, 1], [2, 1, 1]]
        >>> sorted(permute_unique_set([1,2,1,1]))
        [[1, 1, 1, 2], [1, 1, 2, 1], [1, 2, 1, 1], [2, 1, 1, 1]]
        >>> sorted(permute_unique_set([1,2,3]))
        [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

    """

    uniq_perms = {tuple()}  # Initialized with the empty set

    # for each iteration, size of current permutations will increase by 1
    # => at the end of the algorithm, size of each permutation will be equal to len(nums)
    for curr_num in nums:

        # Bisect-left: Insert `curr_num` to the left of insertion_idx in perm
        # (increases size of permutation by 1)
        uniq_perms = {
            # tuples for hashability
            (*perm[:insertion_idx], curr_num, *perm[insertion_idx:])
            for perm in uniq_perms
            for insertion_idx in range(len(perm) + 1)
        }

    return [list(permutation) for permutation in uniq_perms]