def permute_unique_i(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_i([1,1,2]))
        [[1, 1, 2], [1, 2, 1], [2, 1, 1]]
        >>> sorted(permute_unique_i([1,2,1,1]))
        [[1, 1, 1, 2], [1, 1, 2, 1], [1, 2, 1, 1], [2, 1, 1, 1]]
        >>> sorted(permute_unique_i([1,2,3]))
        [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

    """
    """ALGORITHM"""

    def get_last_valid_insertion_idx(perm, item):  # nosemgrep
        """Restrict insertion past the first occurrence of a duplicate value (if one exists)"""
        # equivalent to `(perm + [item]).index(item)`
        try:
            return perm.index(item)
        except ValueError:
            return len(perm)

    # DS's/res
    uniq_perms = [[]]

    # Build unique permutations
    # increasing the permutation size by 1 at each iteration

    for curr_num in nums:
        np = []
        for perm in uniq_perms:
            for insertion_idx in range(
                get_last_valid_insertion_idx(perm, curr_num) + 1
            ):
                new_perm = perm.copy()
                new_perm.insert(insertion_idx, curr_num)
                np.append(new_perm)

        uniq_perms = np

    return uniq_perms