#count data and remember repetitioni

1 messages · Page 1 of 1 (latest)

barren oracleBOT
#

<@&987246399047479336> please have a look, thanks.

barren oracleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

fleet trellis
#

Why don't you try Map of entries,

Map<String, Integer> map = new HashMap<String, Integer>(); 
for (String entry: entries){
Integer count = map.get(entry);
map.put(entry, (count == null)? 1: count+1);```

Max value can be obtained using
`int maxValue = Collections.max(map.values());`
tawny saffron
#

entries does not exist

#

are you trying to loop through all the keys?

#

if so use map.keySet()

dusty condor
#

Is entries your array of Strings?

#

If this is being read line by line from the file I'd use Map.compute to compute the value (avoiding the lookup and put as non-atomic)

If the lines are already in memory as a String[] I'd stream over the array, collecting into a map, using a merge-function to accumulate counts. On a really big array, running it in parallel should improve performance.

In either case, the Integer count will likely evade escape analysis, so in a performance sensitive scenario I'd use a mutable int container to accumulate the count.

Finding the max is then a stream over the map.entries, looking for the max.

fleet trellis
#

Actually Talden is right, stream is a better method for bigger datasets,

Map<String,Long> map = Arrays.stream(entries).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
dusty condor
#

Computing entries is something like

   for (...) {
       map.compute(entry, (e, c) -> c == null ? 1 : (c + 1));
   }

Streaming wiould be something like

    Map<String, Integer> map = Arrays.stream(entries)
        // .parallel() // may or may not speed up collation
        .collect(Collectors.groupingBy(e -> e, Collectors.countingBy()));

Finding the map is simple a stream using map over the entries with a comparator Comparator.comparing(Map.Entry::getValue, Integer::compare) - or something like that.

#

Ahh yes... counting is a long...