java - Remove and mark duplicates in a string array -


i'm having

string array[] = {"test","testing again", "test"}; 

that want mark , remove duplicates. here output need:

2x test  testing again 

can me this? i've tried set seems doesnt recogize when string inthere.

here code:

set addons = new hashset<string>(); final string[] arr ={"test","testing again", "test"};             (int = 0; < arr.length; i++) {                 log.d(tag, "contains adding " + arr[i]);                  if (addons.contains(arr[i])) {                     //never enters here                     log.d(tag, "contains " + arr[i]);                     addons.remove(arr[i]);                     addons.add("2 x " + arr[i]);                 } else {                     addons.add("1 x " + arr[i]);                 }             } 

you can this:

string[] arr = { "test", "testing again", "test" }; hashmap<string, integer> counter = new hashmap<>(); (int = 0; < arr.length; i++) {     if (counter.containskey(arr[i])) {         counter.put(arr[i], counter.get(arr[i]) + 1);     } else {         counter.put(arr[i], 1);     } } system.out.println("occurrences:\n"); (string key : counter.keyset()) {     system.out.println(key + " x" + counter.get(key)); } 

your example doesn't work because, when find new occurrence of word remove , replace 2x [word], when word comes again contains(...) return false since it's no longer in set.


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -