Drill

ProblemsC# › patterns

Push the empty slots to the end

easypatternsTwo pointersArraysC#

A picking list uses zero for a line that was cancelled. The screen keeps the live lines in order and pushes the blanks to the bottom.

MoveBlanksLast(lines: list<int>) → list<int>

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public List<int> MoveBlanksLast(List<int> lines) {
    
}

Worked examples

CallResult
MoveBlanksLast(new List<int> { 0, 1, 0, 3, 12 })new List<int> { 1, 3, 12, 0, 0 }
MoveBlanksLast(new List<int> { 1, 2, 3 })new List<int> { 1, 2, 3 }
MoveBlanksLast(new List<int> { 0, 0 })new List<int> { 0, 0 }
MoveBlanksLast(new List<int> { })new List<int> { }

Hint

Keep a write index. Walk the list once copying every non-zero value to that index and advancing it; then fill what is left with zeros.

Reference solution in C#
public List<int> MoveBlanksLast(List<int> lines) {
    var outList = new List<int>();
    foreach (var value in lines) {
        if (value != 0) outList.Add(value);
    }
    while (outList.Count < lines.Count) outList.Add(0);
    return outList;
}

The same problem in another language

More patterns problems in C#