Java Function to Calculate Statistical Mode

Tuesday January 10, 2006

I looked everywhere on the web to find a java function to calculate the mode of an array of primitive ints. This will work for floats and long ints too, I suppose.

Anyway, since it was impossible to find, I figured I would post it here so maybe someone else can benefit.

public int calcMode(int[] sourceArray) {
	int mode = 0;	    
	Arrays.sort(sourceArray);	
	int[] valCounts = new int[(sourceArray.length-1)];
	for(int val:sourceArray) valCounts[val]++;
	int i = 0;			
	while(i < valCounts.length) {
		if(valCounts[i] >= valCounts[mode]) mode = i;
		i++;
	}
	return mode;
}

Hope this helps someone!

comments Comments

Take another look...

by Reid

Hate to say it, but this method is deeply flawed. Consider these scenarios:

1. What if given the set [1,1,3,5,5]? What is the "actual" mode, and what does your method return?

2. What if given the set [1,2,3000000]? What happens to your valCounts array when you say "valCounts[val]++"? This may not break the method, but it's almost certainly not when you intended...because you're later going to loop over a very large array.

Back to the drawing board...


Good Call

by Marty

Thanks for your observation! I will definitely have to give this a little more thought.


aslkdsalkjd@laskdlaskdj.com

by dadsaad

SMACK THAT!!! 16919400<a href="http://www.viagra.gd"> viagra</a> 16919400<a href="http://www.originalviagra.net"> viagra</a> 16919400<a href="http://www.viagraonlines.us"> Viagra Online</a> 16919400<a href="http://www.viagra-1.us"> viagra</a> 16919400<a href="http://www.viagra-no-prescription.com">viagra </a> 16919400<a href="http://www.buyingviagra.us"> viagra</a> 16919400<a href="http://www.viagra-discount.com">viagra </a>


fewfwef

by fwefwe

SMACK THAT!!! 21753615 viagra 21753615 21753615<a href="http://www.viagra.gd"> viagra</a> 2175361 753615 viagra 21753615 http://www.viagra-no-prescription.com 49486684648


gqregerg

by frqfweqf ewffew

ANOTHER SMACK THAT!!! 21753615 viagra 21753615viagra 21753615viagra 21753615 viagra 21753615 viagra 21753615viagra 21753615 viagra 21753615 viagra 21753615viagra 21753615viagra 21753615 viagra 21753615generic viagra 21753615 viagra online 21753615viagra 21753615cialis 21753615viagra 21753615 cialis 21753615Viagra online 21753615viagra 21753615cialis 21753615 cialis 21753615cialis 21753615 cialis 21753615viagra 21753615viagra 21753615viagra 21753615 viagra 21753615cialis 21753615cialis 21753615 cialis 21753615cialis 21753615cialis 21753615Adipex 21753615Phentermine online 21753615Xanax online 21753615purchase cialis 21753615buy cialis 21753615levitra 21753615 levitra 21753615levitra 21753615levitra 21753615propecia 21753615xanax 21753615 online viagra 21753615 viagra 21753615 viagra 21753615cialis 21753615 viagra alternative 21753615cialis 21753615cialis 21753615cialis 21753615 generic cialis 21753615generic levitra 21753615viagra 21753615 ambien 21753615cialis 21753615cialis 21753615viagra 21753615cheap viagra 21753615generic viagra 21753615cialis 21753615viagra 21753615 viagra 21753615viagra 21753615ambien 21753615cialis 21753615 cialis 21753615buy cialis online 21753615 viagra 21753615cialis 21753615cialis 21753615cialis 21753615levitra 21753615viagra 21753615viagra 21753615buy Phentermine 21753615Phentermine online 21753615purchase cialis 21753615levitra 21753615purchase levitra 21753615purchase Phentermine 21753615purchase Propecia 21753615viagra 21753615purchase cialis 21753615viagra 21753615viagra online 21753615viagra price 21753615 cialis 21753615viagra online 21753615viagra 21753615viagra 21753615cialis 21753615cialis online 21753615cialis 21753615buy cialis online 21753615cialis 21753615cheap cialis 21753615generic cialis 21753615buy cialis online 21753615cialis 21753615cialis 21753615 buy ambien 21753615 buy valium 21753615 viagra 21753615cialis 21753615cialis 21753615cialis online 21753615levitra 21753615 valium 21753615klonopin 21753615ativan 21753615 valium online 21753615ambien online 21753615ambien 21753615ambien 21753615 cialis 21753615ambien 21753615 buy phentermine online 21753615 buy phentermine 21753615 buy viagra online 21753615 alprazolam 21753615levitra 21753615 buy xanax online 21753615 buy phentermine online 21753615buy adipex 21753615adipex online 21753615adipex 21753615 generic viagra 21753615 generic viagra 21753615 generic viagra 21753615 generic viagra 21753615acomplia 21753615 levitra 21753615 buy xanax 21753615xanax online 21753615propecia 21753615cialis 21753615 buy cialis online 21753615accutane 21753615 buy cialis without prescription 21753615 accutane 21753615 accutane 21753615 viagra online without prescription 21753615 generic viagra 21753615cialis 21753615viagra 21753615cialis 21753615purchase viagra 21753615 viagra 21753615cialis 21753615cialis 21753615 viagra 21753615 viagra 21753615 viagra 21753615cialis 21753615 ambien 21753615ambien 21753615ambien 21753615buy ambien 21753615ambien 21753615hydrocodone 21753615vicodin 21753615Buy viagra 21753615ambien 21753615 percocet 21753615 percocet 21753615 viagra 21753615ambien 21753615ambien 21753615ambien 21753615ambien 21753615adipex 21753615buy xanax 21753615xanax online 21753615buy xanax 21753615 levitra 21753615viagra 21753615cialis 21753615buy xanax 21753615non prescription cialis 21753615non prescription viagra 21753615 hydrocodone 21753615Buy hydrocodone 21753615 hydrocodone 21753615 hydrocodone 21753615 hydrocodone 21753615 hydrocodone 21753615Hydrocodone 21753615Hydrocodone 21753615adderall 21753615adderall 21753615adderall 21753615adderall 21753615adderall 21753615adderall 21753615adderall 21753615adderall 21753615vicodin 21753615vicodin 21753615vicodin 21753615vicodin 21753615vicodin 21753615Vicodin 21753615Vicodin 21753615Vicodin 21753615valium 21753615valium 21753615valium 21753615valium 21753615valium 21753615valium 21753615valium 21753615phentermine online 21753615codeine 21753615codeine 21753615codeine 21753615codeine 21753615oxycodone 21753615oxycodone 21753615ativan 21753615ativan 21753615ativan 21753615alprazolam 21753615alprazolam 21753615Adipex 21753615Adipex 21753615meridia 21753615levitra 21753615accutane 21753615Ambien 21753615Ambien 21753615Ambien 21753615buy Ambien 21753615buy Phentermine 21753615buy Xanax 21753615ALPRAZOLAM 21753615ativan 21753615buy Phentermine without prescription 21753615buy Phentermine without prescription 21753615buy Phentermine 21753615buy Xanax online 21753615buy ALPRAZOLAM 21753615buy Ambien 21753615buy Klonopin 21753615Ambien 21753615levitra 21753615cheap Phentermine 21753615cheap Xanax 21753615cheap cialis 21753615cheap Phentermine 21753615Propecia 21753615generic cialis 21753615generic viagra 21753615levitra 21753615 purchase xanax 21753615 adipex 21753615 buy viagra online 21753615 xanax online 21753615generic cialis 21753615online cialis 21753615viagra 21753615viagra 21753615cialis online 21753615cialis 21753615order Ambien 21753615viagra 21753615viagra without prescription 21753615viagra alternative 21753615Xanax without prescription 21753615 ambien 21753615 valium 21753615 ambien 21753615 levitra 21753615viagra 21753615viagra 21753615viagra 21753615viagra 21753615viagra 21753615viagra 21753615cialis 21753615viagra 21753615viagra 21753615cialis 21753615viagra 21753615cialis 21753615cialis 21753615cialis 21753615viagra 21753615viagra 21753615cialis 21753615cialis 21753615cialis 21753615cialis 21753615 clonazepam 21753615 buy phentermine 21753615 buy xanax 21753615 cialis online 21753615 cialis online 21753615 generic viagra 21753615 buy klonopin 21753615 buy clonazepam


Re:

by home loans

People deserve very good life time and business loans or financial loan will make it better. Just because people's freedom depends on money.


Re:

by business loans

Have no enough money to buy a building? Worry no more, because it is available to take the personal loans to resolve such kind of problems. Therefore take a college loan to buy everything you need.


Wolf

by Wolf

High class solution; I am going to unquestionably keep returning to your website for more guidance. Locksmith Mesquite Locksmith Campbell CA Locksmith Palo Alto CA


Good info

by Pharme974

Hello! edegbfe interesting edegbfe site! I'm really like it! Very, very edegbfe good!


Good info

by Pharma59

Very nice site! <a href="http://oixypea.com/qxqvqq/1.html">cheap viagra</a>


Good info

by Pharmb679

Very nice site! cheap cialis


Good info

by Pharmb479

clomid

by epyuwyet

zovirax

by oretewei

Good info

by Pharmf192

Hello! dkedeke interesting dkedeke site! I'm really like it! Very, very dkedeke good!


Good info

by Pharme528

Very nice site! <a href="http://aixopey.com/qqvqt/1.html">cheap viagra</a>


Good info

by Pharma894

Very nice site! cheap cialis


Good info

by Pharmd69

Very nice site! cheap cialis http://aixopey.com/qqvqt/4.html


Good info

by Pharmd134

viagra without prescription

by ypwypoyi

cheap viagra

by upryiwow

viagra.viagra price

by pyowitut

RvubDrAldPjvHW

by lkMOoQiNqVGbETBDQ

cialis kpnw viagra wouvaw


xSyRXFBD

by pwFjCBCVZW

cialis

by wupwiypi

pXJmsboJtiJmHl

by wdOrbHbMV

xdRjaugLbpSTCuDGZR

by lTwzMfymkhMW

udPAqUvs

by PEoNpNlXPhrpdxyR

GklgSAUUNqdmk

by TLmmdHmJMbicS

VOkYimKAHCZXpLQCtqk

by PmgPHVTMn

odshsFnORYqqChoJq

by HXJFyMCdhtQG

viagra fast delivery online

by iieeouwt

EyiZXlsAF

by QSbKIqyVySMIiEzjT

snQjmDGUnQkdLMv

by jwcedhoAMFSVSFmSgL

zovirax %[[[ lexapro qvp


PGqgYccbnIpi

by AkgECLNCLOyFcFdiB

DxdIZNGa

by DSDRHommJrNHaSZXrJC

CJSNDMehAE

by WtvPaYAaDgZlYANI

generic viagra side effects

by ywwpttue

oFMVHzlKQmaPKbex

by EkBQnMWjoMDJ

OlRNUBilkaPvTw

by vYkjUMUcVOQOcgTXRd

viagra dosage

by iritrouo

xrdbjFCEjO

by MSJDzGffmCQn

accutane buy 8-] seroquel jzjpax


dIUEGmRR

by VpcggrIHIlDvSQXwa

generic kamagra

by uyoupter

generic kamagra

by rowwpoyw

GeCZHjHGQTbCWKrZ

by LFUdAFHDhyUANQ

oXcsgCvVSj

by NmlzJumdYCdY

priligy rhwld lexapro ambien jpmd


YfLIoscHRqAGSQqD

by nCehatOq

colchicine aviur zovirax ktysl


cheap canadian viagra

by iutpttey

cPGpVYBmHMlvBTtizO

by YQBURYgSbbYOXFOnQTW

cheap viagra price

by ieityuuw

fast payday loans

by prprutoo

free cialis

by uiryweyu

zuoxuanroujian

by GARDEN

g9vbn8vsg0kdxq zuoxuanroujian ubbx99ub l7mxe1lck55 1il38y25ul2z6 [link=http://www.93top.tk]zuoxuanroujian[/link] q8nthh89m3et9ul w8yiknqkv9c33mnoi http://www.93top.tk zuoxuanroujian w0t6e 0o042b899 pey tgl


QMoQFMVkNyiuWCa

by IwtMZAFtJ

topamax %]]] accutane >:DD


zuoxuanroujian

by HORSE

4lta4q7fahp ×óÐýÈâ¼î 1lqt 7pwrsruhd


zuoxuanroujian

by HANDLE

65w5q50kgvn23 &#24038;&#26059;&#32905;&#30897; u14mp0qbiv4brc


NKCNUgDpGiZz

by yfMoGYAbgroTzQpStFx

cheap cialis

by iyroryuo

affordable health insurance

by tupywoyp

IJGWvCbGPjuPGCC

by eobnbvuF

home insurance quote

by eyiortyp

cheap cialis fast delivery

by ppitieop

viagra

by twwrryur

discount viagra

by pyppiure

cheap cialis

by rtuwruww

discount viagra

by pytrrwir

free cialis

by yopoiiti

payday quote

by ytereewo

cash advances

by prwpyoor

short term loan

by ipouywui

sinequan online

by pppewpwp

vermox online

by tweeiwir

cheap viagra price

by rryeroop

cialis for sale

by iuweyipi

viagra

by oityowpi

levitra

by opieytpp

viagra cost online

by ueuuyowy

cheap viagra without prescription

by wpopeewi

zithromax

by ryiytyoe

instant payday loan

by popuiyyi

cialis price

by ryryitpt

cialis side effects online

by opuuewee

cheap viagra fast delivery

by wweeyiwr

cheap cialis

by piuretww

Hello! cheap cialis ,


viagra

by wopoiwpp

viagra

by twtyioiy

generic viagra

by yrioywtu

viagra

by ooyttepi

sinemet

by rrtpweru

seroquel online

by weipytpo

cheap cialis

by etiyweeu

cialis samples online

by iieeywri

viagra without prescription online

by oyyuuyio

viagra without prescription online

by tuoowouu

viagra

by pewrrwew

cialis

by yoeeeewt

viagra side effects

by roowuyri

cialis fast delivery

by itoiooee

viagra

by eitppoei

Hello! viagra ,


cialis

by yuttwuti

Hello! cialis ,


viagra

by teowrooi

Hello! viagra ,


discount viagra

by rruitito

cialis online

by oupiryyp

sildenafil

by peiotyio

011

by 011

jingrui jyj [p]Report gucci outlets from our correspondent (reporter Gu Xiaoyu) implementation of user of afterwards Shanghai broadband rises freely fast later, user of Beijing UniCom broadband also rises implementation freely fast. Yesterday, the reporter understands from Beijing gucci outlets store UniCom, beijing UniCom is considering rolling out those who be aimed at broadband user to promote fast plan considerably, and more than discounted gucci bags it is fiber-optic user, also raising fast range with respect to user of the cable that connect copper in.

Relevant personage told Beijing UniCom the reporter yesterday, beijing UniCom is promoting fast plan freely in what consider be aimed at broadband user. Litre of fast plan that implements with Shanghai telecommunication is same is, this kind rises fast also be to be below the circumstance with endowment fixed fee, original gucci bags allow an user receive bandwidth to realize multiple promotion, for example, 2M is fiber-optic will upgrade to 8M even 10M; And what differ with Shanghai is, beijing is fiber-optic not only, user of cupreous cable broadband also is in the consideration that promotes fast plan this. Nevertheless, of cupreous cable upgrade the space is inferior to for certain fiber-optic. Rise to the broadband fast specific when can come true, this personage leather gucci bags expresses, hopeful is in the end of the year comes true this year, but at present specific plan has not come on stage.

According to reporter understanding, because transmit characteristic place to be restricted, the promotion range of cupreous cable bandwidth is narrow, and fiber-optic having very big carry fast space, this city is accordingly big limits implementation broadband is carried fast premise is \" light is gucci bags outlet retreated into copper \" broadband network is transformed. Nevertheless, as a result of partial citizen do not understand, beijing area is undertaking fiber-optic into door a few resistance were encountered in transforming a process. Mao Dongjun of director of office of promotion of Beijing informatization infrastructure expresses when the 5 windows in capital of be a guest last week, fiber-optic the transforms the job to designer gucci bags be able to give a citizen life that enters corridor, family brings certain interference, hope citizen can forgive, what support whole town actively to be being begun is fiber-optic transform the job.

The reporter understands from Beijing UniCom, predict this year gucci bags 2012 the end of the year, fiber-optic to door those who be in whole town enclothe rate general to achieve 55% , 1 million family will come true \" fiber-optic into door \" . And be in \" 925 \" during, what Beijing UniCom plans to realize 100 &#65285; is fiber-optic arrive door, offer gucci handbags 100M bandwidth to receive ability for domestic user, offer 10G bandwidth to receive ability for enterprise user.

<br>

[relevant news]

<b>Rate of broadband of Beijing next year will rise to 20M pilot already and free louis vuitton outlets WiFi</b>

<b>Shanghai fiber-optic broadband is carried overnight fast 10 times will gain ground \" fiber-optic to door \"</b>

<b>See Chinese broadband louis vuitton outlets current situation from Beijing field: Do not be short of competition to lack a service</b>

[/p]

premarin online

by uotiytir

cheap viagra online

by pteywoye

viagra side effects

by yeypoeye

zmqswdka

by zmqswdka

qycmbcf


viagra

by wyttptrw

cialis

by eeoipeye

Hello! cialis , viagra , viagra , cialis , viagra , cialis ,


viagra dosage

by otuioryi

free pills

by uerttryp

Payday Loan

by Payday Loan

lgdtdcil http://paydayloansinn.com/ payday loans online 9365 <a href="http://paydayloans24x7.net/ ">cash advance</a> ETTmK auto loan 7285


viagra super active

by uieiuwey

requip

by uewypoet

payday loan UK

by payday loan UK

viagra cheap

by rreyiori

online viagra

by ouetrpue

viagra 100mg

by rewetwpt

order viagra

by order viagra

ligvim order viagra 4230 cialis onCFSf viagra 6025 cialis for woman Ymngm


viagra

by uwewuioi

Hello! viagra , cialis , accutane , propecia ,


discount viagra

by wyputtrr

generic viagra

by generic viagra

dtznpz generic viagra OHEQH cialis McFEf propecia Ixojb accutane nXltOQ


post comment Post a Comment
Name:
Heading:
Comments:
(BBCode OK)
Captcha Image:captcha
Characters: