• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Write a method to find number of occurrences per second and minute

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I might be rather stupid here, but I am trying to find way to write a method that will return the number of request in a specific timeframe. The number of requests are in an Array in this form:



This means that there are 4 requests that arrive at second 1 and 2 at second 2. My approach would be to basically work out the duplicates that there are in the array (so basically saying there are 4 occurrences of 1), but then somehow I am not sure how to include the time aspect. I am really looking for some advice on the algorithm, not a proper solution.

Thank you
 
Saloon Keeper
Posts: 14501
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why use a List? Just use a Map with the minute as its key, and the frequency as its value. Such a mapping is called a histogram.

You can use histogram.merge(minute, 1, Integer::sum) instead of what you're doing with resFrequency.add(minute) now.
 
Stephan van Hulst
Saloon Keeper
Posts: 14501
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you already have a list of the key type, and you want to convert it to a histogram, you can convert the list to a Stream and then call Stream.collect() using Collectors.groupingBy() and Collectors.counting().
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic