How to put numbers into an array and sorted by most frequent number in java -
i given question on programming in java , wondering best way of doing it.
the question on lines of:
from numbers provided, how in java display frequent number. numbers was: 0, 3, 4, 1, 1, 3, 7, 9, 1
at first thinking should in array , sorted first maybe have go through loop. on right lines. examples greatly
if numbers small, can frequent value creating array keep track of count each number. algorithm be:
- find maximum value in list
- create integer array of size max + 1 (assuming non-negative values) store counts each value in list
- loop through list , increment count @ index of each value
- scan through count array , find index highest value
the run-time of algorithm should faster sorting list , finding longest string of duplicate values. tradeoff takes more memory if values in list large.
Comments
Post a Comment