Complete this form and hit the "Save Changes" button!
7 ratings
Are you sure you want to delete this code? Please type DELETE in the box below to confirm.
Are you sure you want to verify this code?
Are you sure you want to publish this code?
Are you sure you want to unpublish this code?
Are you sure you want to autogenerate the summary + description for this code?
xxxxxxxxxx
method RemoveElement(nums: array<int>, val: int) returns (newLength: int)
modifies nums
ensures 0 <= newLength <= nums.Length
ensures forall x :: x in nums[..newLength] ==> x != val
ensures multiset(nums[..newLength]) == multiset(old(nums[..]))[val := 0]
{
var i := 0;
var j := 0;
while i < nums.Length
invariant j <= i
invariant i <= nums.Length
invariant old(nums[i..]) == nums[i..];
invariant multiset(nums[..j]) == multiset(old(nums[..i]))[val := 0]
if nums[i] != val {
nums[j] := nums[i];
j := j + 1;
}
i := i + 1;
assert old(nums[..i]) == old(nums[..]);
return j;