Start learning free

LESSON 19 OF 69 · PROJECTS: COMMAND-LINE TOOLS

uniq -d in C#: Printing Only Repeated Lines

Practice Read the full explanation

Build a version of the Unix uniq -d command. Given a list of lines, output only the values whose adjacent run length is greater than one — i.e. lines that actually repeat back-to-back. Each surviving value is printed once (no count, just the value), joined by \n.

Function signature:

public string UniqDups(List<string> lines)

Example:

Input: lines = ["a", "a", "b", "c", "c", "c"]

Output: "a\nc"

When printed:
a
c

The run of "a"s has length 2 and the run of "c"s has length 3, so both survive. The single "b" is dropped.


Track prev and count. When the line changes (or after the loop), flush prev only if count > 1. Compare strings with ==, then join the survivors with string.Join.

if (count > 1) {
    result.Add(prev);
}


The full solution:
class Solution {
    public string UniqDups(List<string> lines) {
        List<string> result = new List<string>();
        int count = 0;
        string prev = "";
        for (int i = 0; i < lines.Count; i++) {
            if (count > 0 && lines[i] == prev) {
                count++;
            } else {
                if (count > 1) {
                    result.Add(prev);
                }
                prev = lines[i];
                count = 1;
            }
        }
        if (count > 1) {
            result.Add(prev);
        }
        return string.Join("\n", result);
    }
}

STEP 1 OF 13
    Ctrl + Enter to check

    C# · PROJECTS: COMMAND-LINE TOOLS

    Uniq V3 in C#, explained

    Problem overview

    UniqDups is the -d flag of uniq. It takes a List<string> of lines and returns the values that repeat back to back, each printed once, joined with \n. A line that appears only once in its run is dropped.

    For ["a", "a", "b", "c", "c", "c"] the run of as has length 2 and the run of cs has length 3, so both are kept, while the single b is dropped. The result is "a\nc". Only adjacent repeats count: in ["a", "b", "a"] no run is longer than 1, so the result is empty.

    Approach

    This is the run-counting idea from the counting lesson with a different output: track the current run with a line (prev) and a length (count), and when a run ends, keep its value only if its length is above 1. The count is no longer printed, so one copy of the value is added, however long the run was.

    A run ends when a different line arrives. The last run has no later line to end it, so it must be tested again after the loop.

    Tracing the example

    With ["a", "a", "b", "c", "c", "c"]: the first a starts a run with count 1. The second a raises it to 2. The b arrives, so the a run has ended with a length of 2, above 1, and "a" is added. The b run starts with a count of 1. The first c ends it: the length is 1, so nothing is added. The c run grows to 3, and since the loop finishes with it open, the final test adds "c". The result is "a\nc".

    Step by step

    1. Create result, an empty List<string> that will hold one copy of each repeated line.
    2. Create count at 0, where 0 means no run has started.
    3. Create prev as the empty string "", meaning no text yet.
    4. Loop over the indexes with for.
    5. Test whether the run continues: count > 0 and lines[i] == prev, with an else branch.
    6. If so, add 1 to count.
    7. Otherwise the old run is over. Start an if on count > 1: only a run that repeated is worth keeping. At the first line, count is 0, which is not above 1.
    8. Add prev to result inside it.
    9. Start the next run: set prev to lines[i].
    10. Set count to 1.
    11. After the loop, test the last run with count > 1.
    12. Add prev in that branch.
    13. Return string.Join("\n", result).

    The solution in C#

    class Solution {
        public string UniqDups(List<string> lines) {
            List<string> result = new List<string>();
            int count = 0;
            string prev = "";
            for (int i = 0; i < lines.Count; i++) {
                if (count > 0 && lines[i] == prev) {
                    count++;
                } else {
                    if (count > 1) {
                        result.Add(prev);
                    }
                    prev = lines[i];
                    count = 1;
                }
            }
            if (count > 1) {
                result.Add(prev);
            }
            return string.Join("\n", result);
        }
    }
    

    The details worth knowing:

    • Why count > 0 comes first. The starting prev is "", which is also a valid line. The check on count says whether a run has really started, so the empty starting text is never mistaken for an empty input line.
    • One copy per run. A run of five equal lines adds prev once, at the moment the run ends.
    • The repeated line is output when the run ends, so the results come out in the order the runs appear in the input.
    • String equality. == on two strings compares their characters, and it is case-sensitive: ["a", "A"] are two runs of 1.
    • An empty line is a value like any other. A run of two empty lines is added as "", and the joined result of a list that holds only one empty string is also "", so the text alone does not show whether the run was there.

    Comparing with the other uniq lessons

    The three versions share one skeleton. The first keeps the first line of each run and has no counter. The counting version writes every run with its length. This one needs the length only to answer a yes-or-no question, whether it is above 1, which is why it keeps count but never prints it. If you understand where each version writes its output, at the moment a run ends and once more after the loop, you can build the others from the same loop, for example a version that prints only the lines that occur once, with the test count == 1.

    Complexity

    Let n be the number of lines. Time: one pass, so it grows linearly with n (times the line length for the comparisons). Space: the result holds at most n / 2 lines, since each repeated run takes at least two input lines, plus the joined string.

    Edge cases and common mistakes

    • Forgetting the last run. For ["a", "a"] the loop never sees a different line, so without the final test the result would be empty instead of "a".
    • Using count > 0 to keep a run. That keeps every line, which is the counting version's behaviour. Here the bar is above 1.
    • Adding every line of a run. The result would contain "a\na". Add prev once.
    • No repeats. ["a", "b", "c"] gives "".
    • Non-adjacent repeats. ["a", "b", "a", "a", "b", "b"] gives "a\nb": the second a run and the b run are the repeats.

    Testing

    • ["a", "a", "b", "c", "c", "c"] gives "a\nc".
    • ["a", "b", "a", "a", "b", "b"] gives "a\nb".
    • ["a", "b", "A"] gives "".
    • [] gives "".
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free