How to display string with string count

I have a string array as follows:

String[] str = new String[]{"foo","bar","foo","car"} 

I need the output as follows:

 bar1car1foo2 

I tried like this:

 String[] someArray = new String[] { "foo","bar","foo","car"}; for(int i=0;i<someArray.length;i++){ int count=0; for(int j=0;j<someArray.length;j++){ if(someArray[i].equals(someArray[j])){ someArray[i] +=count; } } System.out.println(someArray[i]); } 

and my conclusion:

 foo0 bar0 foo0 car0 
+5
source share
5 answers

One option is to use Map<String, Integer> , where the keys represent individual lines, and the map value is a counter for each of them.

So you can do something like:

 Map<String, Integer> stringsWithCount = new TreeMap<>(); for (String item : str) { if (stringsWithCount.contains(item)) { stringsWithCount.put(item, stringsWithCount.get(item)+1)); } else { stringsWithCount.put(item, 0); } } 

And then you can iterate the map upon completion:

 for (Entry<String, Integer> entry : stringsWithCount.entrySet()) { 

and create a row of results.

It was like an old school implementation; if you want to be fantastic and surprise your teachers, you can go for a Java8 / lambda / stream solution:

 Arrays.stream(str) .collect(Collectors .groupingBy(s -> s, TreeMap::new, Collectors.counting())) .entrySet() .stream() .flatMap(e -> Stream.of(e.getKey(), String.valueOf(e.getValue()))) .collect(Collectors.joining()) 

But, of course, you should be able to explain this piece of code.

+5
source
  public static void main(String args[]) { String[] strArray = new String[] { "foo","bar","foo","car"}; countduplicate(strArray ); } public static void countduplicate(String avalue[]) { String str = ""; Set<String> set = new HashSet<String>(); for (int i = 0; i < avalue.length; i++) { set.add(avalue[i]); } Iterator<String> iterator = set.iterator(); List<String> list = new ArrayList<String>(); while (iterator.hasNext()) { int count=0; str =iterator.next(); for (int i = 0; i < avalue.length; i++) { if(str.equals(avalue[i])){ count++; } } list.add(str+count); } for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)); } } 
+2
source
 import java.util.Set; import java.util.TreeSet; public class StringCount { public static void main(String[] args) { String[] str = new String[]{"foo","bar","foo","car","foo","car","bar","bar"}; Set<String> set= new TreeSet<String>(); for(String a : str){ int c=0; for(int i=0;i<str.length;i++){ if(a.equals(str[i])){ c=c+1; } } set.add(a+c); } System.out.println(set); } } TreeSet is used to get the sorted elements. Output: [bar3, car2, foo3] 
+2
source

Here is my solution:

 public static void countAndPrint(String[] elements) { Map<String, Integer> map = new TreeMap<String, Integer>(); for (String item: elements) { int occurrences = map.containsKey(item)? map.get(item) + 1: 1; map.put(item, occurrences); } for(String item: map.keySet()) { System.out.print(item); System.out.print(map.get(item)); } } public static void main(String args[]) { String[] elements = new String[]{"foo","bar","foo","car"}; countAndPrint(elements); } 
+1
source

You can use the SortedBag from Eclipse Collections to solve this problem.

 String[] str = {"foo","bar","foo","car"}; Bag<String> bag = SortedBags.mutable.with(str); bag.forEachWithOccurrences(((each, occurrences) -> System.out.print(each + occurrences))); 

Outputs: bar1car1foo2

Note. I am a committer for Eclipse collections.

+1
source

All Articles