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:

  1. iterate on strings identify lines contain braces.
  2. in other words: identify strings "x (n)"
  3. then, each "different" x found, can iterate list again; can find occurrences of "x", x (1)", .. , on
  4. doing allow detect maximum n each of xes.
  5. 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

Popular posts from this blog

Formatting string according to pattern without regex in php -

c - zlib and gdi32 with OpenSSL? -

java - inputmismatch exception -