| Author |
collection
|
vivek shankare gowda
Greenhorn
Joined: Jul 13, 2011
Posts: 24
|
|
|
if list has following elements 1,1,1,5,7,7,7,7,7,4,4... how to display the output as 1(3) 5(1) 7(5) 4(2).. here program is displaying only unique elements and counting the duplicate values
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
use Map<identity, count++>
|
 |
vivek shankare gowda
Greenhorn
Joined: Jul 13, 2011
Posts: 24
|
|
|
can you please elaborate the code..
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3865
|
|
|
Don't worry about the code to begin with. How would you do it by hand? Write down the steps you'd need to do to explain to someone else exactly how to do this. Once you've done that it's a lot simpler to convert into code. But until you understand how to solve it, this isn't a programming problem.
|
 |
vivek shankare gowda
Greenhorn
Joined: Jul 13, 2011
Posts: 24
|
|
as i understood we can use hashset object to avoid duplication
ArrayList list = new ArrayList(new HashSet(list));
if we use collections.frequency(list,1); it gives 1 number occurences..
is it a right approach??
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
The idea is you write it down in English or any other natural language. You don't think in terms of java objects or collections. You think in terms of paper and pencil.
For example:
read each element, one by one.
each time i read one, look and see if it is written down on my piece of paper
if not, write it down on the next empty line
put a 'tick' mark next to the number (i.e. increase the count by one)
etc
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
It's a good approach if you want to find the distinct entries from the list. It's not such a good approach if you want to know how often each of those distinct entries appears in the list.
So now go back to your original requirements and see if it's a good approach.
|
 |
vivek shankare gowda
Greenhorn
Joined: Jul 13, 2011
Posts: 24
|
|
|
can you please explain the steps to know how often those distinct entries appear in the list..
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
vivek shankare gowda wrote:can you please explain the steps to know how often those distinct entries appear in the list..
You've received solid advice that will lead you to the solution. Why are you ignoring it?
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
|
We want to help you. At this site we feel that guiding someone to their own learning is a lot better than just giving them an answer. If the root of your difficulty is simply not knowing Java well enough to grapple with this problem, then it might be a good idea to work through some tutorials like the ones on Oracle's web site: http://docs.oracle.com/javase/tutorial/java/index.html and http://docs.oracle.com/javase/tutorial/collections/TOC.html
|
 |
vivek shankare gowda
Greenhorn
Joined: Jul 13, 2011
Posts: 24
|
|
|
Thank you for the reply..first i will go through those tutorials..
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
|
|
vivek shankare gowda wrote:Thank you for the reply..first i will go through those tutorials..
And then read the StopCoding page.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
vivek shankare gowda
Greenhorn
Joined: Jul 13, 2011
Posts: 24
|
|
|
awesome page.. thanks all again for wonderful tips and suggestions
|
 |
 |
|
|
subject: collection
|
|
|