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
- Create
result, an emptyList<string>that will hold one copy of each repeated line. - Create
countat 0, where 0 means no run has started. - Create
prevas the empty string"", meaning no text yet. - Loop over the indexes with
for. - Test whether the run continues:
count > 0andlines[i] == prev, with anelsebranch. - If so, add 1 to
count. - Otherwise the old run is over. Start an
ifoncount > 1: only a run that repeated is worth keeping. At the first line,countis 0, which is not above 1. - Add
prevtoresultinside it. - Start the next run: set
prevtolines[i]. - Set
countto 1. - After the loop, test the last run with
count > 1. - Add
previn that branch. - 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 > 0comes first. The startingprevis"", which is also a valid line. The check oncountsays 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
prevonce, 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 twostrings 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 > 0to 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". Addprevonce. - No repeats.
["a", "b", "c"]gives"". - Non-adjacent repeats.
["a", "b", "a", "a", "b", "b"]gives"a\nb": the secondarun and thebrun 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"".
