Filter or compact an array in place with a read pointer and a write pointer.
values[0..write-1] holds exactly the kept values among values[0..read-1], in their original order, and write never passes read.
Use it when you must remove or compact values in place and keep the order of the rest. Signal words: in place, remove, compact, deduplicate, O(1) extra memory, keep the order, without a second array.
Start: read and write both at 0.
Step: read visits every value. If you keep it, copy it to slot write and move write on. If not, only read moves.
Stop: when read reaches the end. Return the kept prefix, values[0..write−1].
Keep at most m copies of each value in sorted data: compare the value at read with values[write − m], in the kept prefix.
It needs an output no longer than the input, so write never passes read.
Cost: each value is read once and copied at most once, so O(n) time and O(1) extra space.
def remove_value(values, unwanted):
# [1] Nothing is kept yet, so write starts at the front.
write = 0
# [2] Read visits every value once, from left to right.
for read in range(len(values)):
# [3] Keep the value only if it is not the unwanted one.
if values[read] != unwanted:
# [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read]
write += 1
# [5] The kept prefix, values[0..write-1], is the answer.
return values[:write]public int[] removeValue(int[] values, int unwanted) {
// [1] Nothing is kept yet, so write starts at the front.
int write = 0;
// [2] Read visits every value once, from left to right.
for (int read = 0; read < values.length; read++) {
// [3] Keep the value only if it is not the unwanted one.
if (values[read] != unwanted) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return Arrays.copyOf(values, write);
}vector<int> removeValue(vector<int>& values, int unwanted) {
// [1] Nothing is kept yet, so write starts at the front.
int write = 0;
// [2] Read visits every value once, from left to right.
for (int read = 0; read < (int) values.size(); read++) {
// [3] Keep the value only if it is not the unwanted one.
if (values[read] != unwanted) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return vector<int>(values.begin(), values.begin() + write);
}function removeValue(values, unwanted) {
// [1] Nothing is kept yet, so write starts at the front.
let write = 0;
// [2] Read visits every value once, from left to right.
for (let read = 0; read < values.length; read++) {
// [3] Keep the value only if it is not the unwanted one.
if (values[read] !== unwanted) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return values.slice(0, write);
}def dedupe_sorted(values, m):
# [1] Nothing is kept yet, so write starts at the front.
write = 0
# [2] Read visits every value once, from left to right.
for read in range(len(values)):
# [3] Keep the value unless the kept prefix already ends with m copies of it: compare m slots back from write.
if write < m or values[read] != values[write - m]:
# [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read]
write += 1
# [5] The kept prefix, values[0..write-1], is the answer.
return values[:write]public int[] dedupeSorted(int[] values, int m) {
// [1] Nothing is kept yet, so write starts at the front.
int write = 0;
// [2] Read visits every value once, from left to right.
for (int read = 0; read < values.length; read++) {
// [3] Keep the value unless the kept prefix already ends with m copies of it: compare m slots back from write.
if (write < m || values[read] != values[write - m]) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return Arrays.copyOf(values, write);
}vector<int> dedupeSorted(vector<int>& values, int m) {
// [1] Nothing is kept yet, so write starts at the front.
int write = 0;
// [2] Read visits every value once, from left to right.
for (int read = 0; read < (int) values.size(); read++) {
// [3] Keep the value unless the kept prefix already ends with m copies of it: compare m slots back from write.
if (write < m || values[read] != values[write - m]) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return vector<int>(values.begin(), values.begin() + write);
}function dedupeSorted(values, m) {
// [1] Nothing is kept yet, so write starts at the front.
let write = 0;
// [2] Read visits every value once, from left to right.
for (let read = 0; read < values.length; read++) {
// [3] Keep the value unless the kept prefix already ends with m copies of it: compare m slots back from write.
if (write < m || values[read] !== values[write - m]) {
// [4] Copy it to the write slot and advance write; write never passes read.
values[write] = values[read];
write++;
}
}
// [5] The kept prefix, values[0..write-1], is the answer.
return values.slice(0, write);
}i = 0
while i < len(scans):
if scans[i] == -1:
scans.pop(i)
i += 1 # bug: the next scan has slid into slot i, and this skips itif read < m or values[read] != values[read - m]: # bug: compare with the kept prefix, values[write - m]This version runs Template 1 and then returns all of values.
Your team lead brings a request from the label team. An oversized parcel needs two labels, so its code must appear twice in the print buffer. The buffer [48, 31, 57, 0] holds three codes and one spare slot, and 31 is oversized. The printer needs [48, 31, 31, 57].
systemdesignmastery.dev/dsa/cheatsheets/read-write-pointers