Problem overview
The method uniqDups takes a List<String> of lines and returns only the values whose adjacent run is longer than one line, which are the lines that really repeat back to back. Each such value appears once, with no count, and the values are joined by \n. This is the -d flag of the Unix uniq command.
For ["a", "a", "b", "c", "c", "c"] the result is "a\nc". 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.
Approach: measure each run, keep the long ones
The method has the same shape as the one that counts runs. Two variables describe the run the loop is in: prev, its text, and count, its length so far. A run is complete when a different line arrives. At that moment the method decides whether to keep it: if count is above 1, the run was repeated, so prev goes into the result once.
The last run needs special care. No line comes after it, so the loop never completes it, and the same test runs again after the loop.
Here is the state after each iteration for the example, from a real run:
| i | line | count | prev | result |
|---|---|---|---|---|
| 0 | a | 1 | a | [] |
| 1 | a | 2 | a | [] |
| 2 | b | 1 | b | [a] |
| 3 | c | 1 | c | [a] |
| 4 | c | 2 | c | [a] |
| 5 | c | 3 | c | [a] |
The run of "a"s is added when b arrives at index 2. The run of "c"s is still open after index 5. The test after the loop adds it, which gives [a, c].
Step by step
- Create
result, an emptyArrayListin aList<String>variable. It holds one copy of each repeated line. - Create
count, anintstarting at 0, where 0 means no run has started. - Create
prev, aStringset tonull, which will remember which line the current run is made of. - Loop with an index from 0 while
i < lines.size(). - Test for a continuing run:
count > 0 && lines.get(i).equals(prev), followed by anelse. - Continue the run: in the
ifbranch,count++. - Check whether the finished run was repeated: in the
elsebranch,if (count > 1). A run of one line was not repeated. For the first linecountis 0, which is not above 1 either, so nothing is added for a run that does not exist. - Keep one copy: inside that
if,result.add(prev). One copy is enough however long the run was. - Start the next run: set
prevtolines.get(i), because the line that ended the old run starts the new one. - Set
count = 1, since the new run has one line so far. - Test the last run after the loop with the same
if (count > 1). - Add it with
result.add(prev). - Join and return with
String.join("\n", result).
The solution in Java
class Solution {
public String uniqDups(List<String> lines) {
List<String> result = new ArrayList<>();
int count = 0;
String prev = null;
for (int i = 0; i < lines.size(); i++) {
if (count > 0 && lines.get(i).equals(prev)) {
count++;
} else {
if (count > 1) {
result.add(prev);
}
prev = lines.get(i);
count = 1;
}
}
if (count > 1) {
result.add(prev);
}
return String.join("\n", result);
}
}
The Java details worth a closer look:
nullas "nothing yet".previs a reference type, so it can holdnullbefore the first line.lines.get(i).equals(prev)is safe with anullargument and returns false, and thecount > 0test already rules out the first iteration.equalsagainst==. Run detection compares the text of two strings, so it needsequals. The==operator compares references.- The duplicated test. The condition
count > 1appears twice, inside the loop and after it. The repetition is the price of a loop that only reacts when a different line arrives: the last run never meets one. - Imports. A standalone file needs
import java.util.*;forListandArrayList.
Complexity
Time: O(m), where m is the total number of characters: each line is compared once with prev, and the work for a finished run is constant.
Space: O(n + m). The result list holds up to one reference per run, and the joined string is a new copy of the kept text.
Edge cases and common mistakes
- No repeats.
["a", "b", "c"]gives"", and so does an empty list. - Only a repeated pair.
["x", "x"]gives"x". Nothing arrives after the pair, so only the test after the loop adds it. - Forgetting the last run.
["b", "a", "a"]should give"a". Without the code after the loop it would give"". - Not adjacent.
["a", "b", "a"]gives"", since the twoas are never neighbours. - Two separate runs of the same value.
["a", "a", "b", "a", "a"]gives"a\na", because thearuns are different runs and each one is reported once. - Empty lines.
["", "", "a"]gives"". The repeated empty line would be a value, but the result is a single empty string, so it joins to""and looks like no output. - Testing
count >= 1. That keeps every run and turns the method into the one that drops adjacent duplicates. - Adding inside the
ifbranch. Addingpreveach timecountgrows would put one copy per extra line, not one per run.
Testing
["a", "a", "b", "c", "c", "c"]gives"a\nc".["a", "b", "c"]gives"".["x", "x"]gives"x".["b", "a", "a"]gives"a".["a", "a", "b", "a", "a"]gives"a\na".
