java - How to remove similar named strings in a list? -
given list/array of strings:
document document (1) document (2) document (3) mypdf (1) mypdf myspreadsheet (1) myspreadsheet myspreadsheet (2)
how remove duplicates retain highest copy number?
ending result be:
document (3) mypdf (1) myspreadsheet (2)
you put in broad question, here comes unspecific (but nonetheless) "complete" answer:
- iterate on strings identify lines contain braces.
- in other words: identify strings "x (n)"
- then, each "different" x found, can iterate list again; can find occurrences of "x", x (1)", .. , on
- doing allow detect maximum n each of xes.
- push "maximum" "x (n)" results list.
in other words: takes such simple receipt solve problem; takes time turn these pseudo-code instructions real code.
for record: if layout of file shown above, things become bit easier - seems numbers increasing. mean is:
x (1) x (2) x (3)
is easier treat than
x (1) x (3) x (2)
as in case, seems save assume last x(n) contains largest n. makes using hashmap (as suggested cainiaofei) nice solution.
Comments
Post a Comment