Problem overview
The function uniqDups(lines []string) string is the -d flag of Unix uniq. It outputs only the lines that repeat back to back: a value is printed, once, if its run of adjacent equal lines is longer than one. The output lines are joined by \n, with no counts.
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 printed once, while the single b is dropped. The result is "a\nc".
Approach
The structure comes from the run-counting version: prev holds the text of the current run and count its length so far. The difference is what happens when a run ends. Instead of formatting a line with its count, the loop asks one question: was the run repeated, that is, is count above 1? If so, prev is appended once, however long the run was.
A run can only be judged when it is over, and the last run has no line after it to end it. So the same test is repeated after the loop for the run that is still open.
Tracing a longer input
Take ["a", "b", "b", "b", "c", "c", "d"]. The loop holds prev and count for the current run and appends a value when the next different line arrives:
| Line | prev after | count after | result after |
|---|---|---|---|
"a" | "a" | 1 | empty |
"b" | "b" | 1 | empty (the run of a had length 1) |
"b" | "b" | 2 | empty |
"b" | "b" | 3 | empty |
"c" | "c" | 1 | ["b"] |
"c" | "c" | 2 | ["b"] |
"d" | "d" | 1 | ["b", "c"] |
After the loop the open run is d with count 1, which is not above 1, so nothing more is added. The result is "b\nc".
Step by step
- Create the empty
resultslice for one copy of each repeated value. - Start
countat 0, meaning no run has started, - and
prevas"", the text of the current run. - Loop by position with
for i := 0; i < len(lines); i++. - Check for a continuing run with
count > 0 && lines[i] == prev, and write theelsebranch. - Extend the run in the
ifbranch:count++. - In the
elsebranch, test the run that just ended:count > 1means it repeated. At the first line,countis 0, so the test fails and nothing is written for a run that doesn't exist. - Append
prevonce inside that test. - Start the new run:
prev = lines[i] - and
count = 1. - After the loop, test
count > 1for the run left open, - and append
previf it passes. - Join with
"\n"and return.
The solution in Go
func uniqDups(lines []string) string {
result := []string{}
count := 0
prev := ""
for i := 0; i < len(lines); i++ {
if count > 0 && lines[i] == prev {
count++
} else {
if count > 1 {
result = append(result, prev)
}
prev = lines[i]
count = 1
}
}
if count > 1 {
result = append(result, prev)
}
return strings.Join(result, "\n")
}
Go details to notice:
- The test is
count > 1, notcount > 0. Using0would print every run, which is the behaviour of the first version. The number 1 is what separates a single line from a repeat. - One copy per run. The code appends
prevonly when the run ends, not each timecountgrows, so a run of 3 appears once. - Variables set by
:=live through the whole function.countandprevare declared before the loop, which is why the check after the loop can still read them. - Empty slice.
strings.Joinof an empty slice is"", so a result without duplicates needs no special case.
The same loop can answer related questions by changing only the test on count. With count > 0 every run is written, and with count > 1 only repeated runs are. The loop keeps track of run boundaries, and the test decides which runs matter. Whichever test is used, it has to be written twice, once for the run that a different line ends and once for the run that the end of the input ends, and the two copies must agree.
Complexity
Time: O(n), where n is the total size of the input: one comparison per line. Space: O(d), where d is the size of the output: one string per repeated run, and the joined result.
Edge cases and common mistakes
- No repeats.
["x", "y"]returns"". - Empty list. The result is
"". - Repeats that are not adjacent. For
["a", "b", "a", "a"], the firstais a run of one, and only the last twoas form a run of two, so the result is"a". A run is printed once per run, so a value that repeats in several separate places is printed once for each:["a", "a", "b", "b", "a", "a"]gives"a\nb\na". - Forgetting the run at the end. For
["q", "q"]the loop never sees a different line, so without the final test the result would be""instead of"q". - Empty lines. They repeat like any other text. In
["", "", "a"]the run of two empty lines is a repeat, so an empty string is appended, and joining a slice with one empty string gives"", the same as when nothing repeats.
Testing
["a", "a", "b", "c", "c", "c"]gives"a\nc".["a", "b", "a", "a"]gives"a".["a", "a", "b", "b", "a", "a"]gives"a\nb\na".["q", "q"]gives"q".["x", "y"]gives"".["a", "b", "b", "b", "c", "c", "d"]gives"b\nc".[]gives"".
