Walk an array once and edit it in place without losing data.
The entries before write are the answer so far, in order; read never falls behind write, so nothing is overwritten before it is read.
Use it when the task is to remove, filter or squeeze entries and keep their order, with no second array. Signal words: remove in place, keep the order, O(1) extra space, overwrite as you go.
Start: read and write are both 0.
Step: read visits each entry once. When the entry stays, copy it to write and move write on by one.
Stop: when read has passed the last entry. The first write entries are the answer, so cut off the rest.
It does not fit an edit that makes the array longer. There, write from the back, or the write pointer runs ahead of read.
Cost: O(n) time, one read for each entry and at most one write for each kept entry, and O(1) extra space.
def remove_value(values, target):
# [1] Work on a copy; write is the place for the next kept entry.
items = list(values)
write = 0
# [2] Read every entry once, from the front.
for read in range(len(items)):
# [3] Keep the entry unless it is the target: copy it to the write place and move the write place on.
if items[read] != target:
items[write] = items[read]
write += 1
# [4] The first write entries are the answer; cut off the rest.
return items[:write]public int[] removeValue(int[] values, int target) {
// [1] Work on a copy; write is the place for the next kept entry.
int[] items = values.clone();
int write = 0;
// [2] Read every entry once, from the front.
for (int read = 0; read < items.length; read++) {
// [3] Keep the entry unless it is the target: copy it to the write place and move the write place on.
if (items[read] != target) {
items[write] = items[read];
write++;
}
}
// [4] The first write entries are the answer; cut off the rest.
return java.util.Arrays.copyOf(items, write);
}vector<int> removeValue(vector<int>& values, int target) {
// [1] Work on a copy; write is the place for the next kept entry.
vector<int> items = values;
int write = 0;
// [2] Read every entry once, from the front.
for (int read = 0; read < (int)items.size(); read++) {
// [3] Keep the entry unless it is the target: copy it to the write place and move the write place on.
if (items[read] != target) {
items[write] = items[read];
write++;
}
}
// [4] The first write entries are the answer; cut off the rest.
items.resize(write);
return items;
}function removeValue(values, target) {
// [1] Work on a copy; write is the place for the next kept entry.
const items = [...values];
let write = 0;
// [2] Read every entry once, from the front.
for (let read = 0; read < items.length; read++) {
// [3] Keep the entry unless it is the target: copy it to the write place and move the write place on.
if (items[read] !== target) {
items[write] = items[read];
write++;
}
}
// [4] The first write entries are the answer; cut off the rest.
return items.slice(0, write);
}def reverse_in_place(values):
# [1] Work on a copy; one pointer at each end.
items = list(values)
left = 0
right = len(items) - 1
# [2] While the pointers have not met, swap the two entries.
while left < right:
items[left], items[right] = items[right], items[left]
# [3] Move both pointers one step towards the middle.
left += 1
right -= 1
# [4] Return the reversed list.
return itemspublic int[] reverseInPlace(int[] values) {
// [1] Work on a copy; one pointer at each end.
int[] items = values.clone();
int left = 0;
int right = items.length - 1;
// [2] While the pointers have not met, swap the two entries.
while (left < right) {
int tmp = items[left];
items[left] = items[right];
items[right] = tmp;
// [3] Move both pointers one step towards the middle.
left++;
right--;
}
// [4] Return the reversed list.
return items;
}vector<int> reverseInPlace(vector<int>& values) {
// [1] Work on a copy; one pointer at each end.
vector<int> items = values;
int left = 0;
int right = (int)items.size() - 1;
// [2] While the pointers have not met, swap the two entries.
while (left < right) {
swap(items[left], items[right]);
// [3] Move both pointers one step towards the middle.
left++;
right--;
}
// [4] Return the reversed list.
return items;
}function reverseInPlace(values) {
// [1] Work on a copy; one pointer at each end.
const items = [...values];
let left = 0;
let right = items.length - 1;
// [2] While the pointers have not met, swap the two entries.
while (left < right) {
[items[left], items[right]] = [items[right], items[left]];
// [3] Move both pointers one step towards the middle.
left++;
right--;
}
// [4] Return the reversed list.
return items;
}This version loops over the indexes and deletes each 0 with del items[i]. After a deletion the next entry slides into place i, and the loop moves on to i + 1, so it never looks at that entry.
This version returns the whole array after the pass. The entries after write are old copies.
This version swaps with items[left] = items[right] and then items[right] = items[left]. The second line copies the value that was written a moment ago, so one value is lost.
This version doubles each 0 in place, reading and writing from the front. When an entry is doubled, write runs ahead of read, and the next writes overwrite entries that were not read yet.
The sync must also write an audit file of the cancelled orders from the original list. An in-place pass overwrites some of them, so the cancelled entries are lost.
systemdesignmastery.dev/dsa/cheatsheets/traversal-and-in-place-edits