d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Counting
Add Reply New Topic New Poll
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Feb 10 2015 06:46pm
So here is my problem statement:
Write a program that reads an unspecified number of integers from a file and finds the integer that has the most occurrences. If several numbers have the most occurrences, all of them should be reported.

I would like to solidify the logic before i jump into it.

Im thinking i read the file into an array, sort it, and count each integer (obviously).
Im wondering if it would be possible to use maybe a linkedhashmap to do this, where each integer is added individually and then i increment a counter in the 2nd position (sorry i dont know the terminology... lhm.put("23", new Integer(i + 1))...the 2nd half of that method call)

Or would there be a simpler way of doing this?
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Feb 11 2015 08:44am
use TreeMap and

Code
Integer value = map.get( key );
if ( value != null ) {
value++;
} else {
value = 1;
}
map.put( key, value );

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll