Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How do you display the most frequent number of elements ?

 
Ranch Hand
Posts: 915
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am stuck at this algorithm which I would like it to return the elements in an array that appear most frequently.

For example, if the array is {1, 22, 1, 3, 22, 0, 1, 22,  3, 3, 24 } then I want the method to return 1, 22, 3.

Here's the code which I need guidance how to modify it to make it return the above.



Thanks.
 
Marshal
Posts: 76468
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code looks weird. It would sitll look weird if you indented it correctly (look at lines 9‑10). Please explain what you are trying to do. And I don't mean, “find the most frequent elements.”. I mean you should explain how many elements you are going to find, and how that code intends to find them. Why is your method intending to find three values? Please explain why you have one loop up to array.length − 1 and another loop starting 1.
 
tangara goh
Ranch Hand
Posts: 915
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That code looks weird. It would sitll look weird if you indented it correctly (look at lines 9‑10). Please explain what you are trying to do. And I don't mean, “find the most frequent elements.”. I mean you should explain how many elements you are going to find, and how that code intends to find them. Why is your method intending to find three values? Please explain why you have one loop up to array.length − 1 and another loop starting 1.



Hi Campbell,

Sorry about the weird code cos I am really not good with Algorithm.  In fact, I only learn how important it is after going to interviews.  And I don't have background in Computer Science so it seems now it dawn on me I may not be quite suitable in this field etc.  Anyway.... Forgive me and the weird code.

Do I need to specify how many elements of most popular frequency by giving it a variable and included that as a parameter in my method ?

I thought there should be a way to not specifying it and the Algorithm should gives me all the values that appear most frequently - in this case the same number of time as the most frequent one.

BTW, that weird code is adapt from one that can give me just one element(value) from an array that appear most frequently.

Hope you could guide me how to go about doing the correct way...like a psuedo code...I really find Algorithm very very tough for me....

Thanks.

 
Saloon Keeper
Posts: 14325
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First you must count how often each element occurs. You do this by iterating over the elements and keeping track of their frequency using Map.merge().

Then you take the entries of the map you just created and group them by their value (a.k.a. the number of occurrences of the key). Use Collectors.groupingBy() for this.

Finally you return the grouping associated with the highest number of occurrences. You can easily do this when you provide groupingBy() a map factory that returns a NavigableMap.
 
Campbell Ritchie
Marshal
Posts: 76468
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:. . . Do I need to specify how many elements of most popular frequency . . .

Do you mean that if there are several elements with the same top requency, you simply return an array containing those elements? So you wouldn't need a particular predefined count.
There is another way of counting, realted to the hints Stephan gave you, in the Java™ Tutorials.

I would suggest you try to work out the algorithm first before writing any code. Also find an introductory book about algorithms, or a course.
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic