JS 依照元素出現數量排序
同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:
var array = ["1", "2", "4", "5", "2", "4", "2", "3"]; var str = array.toString(); var filter = []; var result = []; document.write("原始陣列:" + array + "<br>"); var get = function (str, tar, arr, tmp) { if (str.indexOf(tar) >= 0) { tmp = str.slice(str.indexOf(tar) + 1); arr.push(tar); get(tmp, tar, arr, tmp); } return arr; } for (i in array) { var elm = []; var tmp; var fstr = filter.join(); if (fstr.indexOf(array[i]) >= 0) { continue; } else { var tmp_arr = get(str, array[i], elm, tmp); result.push(tmp_arr.length + ':' + tmp_arr[0]); filter.push(array[i]); } } result.sort(); result.reverse(); //按照出現次數排序,不刪除重複元素 var finishArray = []; for (index in result) { var show = result[index].split(':'); for (i = 0; i < show[0]; i++) { finishArray.push(show[1]); } } document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>"); //按照出現次數排序,刪除重複元素 var finishArray = []; for (index in result) { var show = result[index].split(':'); finishArray.push(show[1]); } document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");
輸出結果:
原始陣列:1,2,4,5,2,4,2,3 按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1 按出現次數排序,刪除重複元素:2,4,5,3,1
I am sure this paragraph has touched all the internet visitors,
its really really good piece of writing on building up new web site.
Аттестат 11 класса купить официально с упрощенным обучением в Москве
купить диплом гигиениста diplomdarom.ru .
Somebody necessarily help to make severely posts I
would state. This is the first time I frequented your website page and to
this point? I amazed with the analysis you made to create this particular
submit amazing. Fantastic process!
The Motive Behind Fabric Corner Sofa Sale Has Become The Obsession Of
Everyone In 2024 right hand fabric corner sofa (http://wiki.iurium.cz)
Why Online Mystery Box Is Much More Hazardous Than You Think Mystery Boxes
10 Of The Top Mobile Apps To Emergency Car Locksmith locksmith near me car key (Ivory)
I do not know if it’s just me or if perhaps
everyone else experiencing problems with your site.
It seems like some of the written text on your posts are running off
the screen. Can somebody else please provide feedback and let
me know if this is happening to them too? This may be a issue
with my web browser because I’ve had this happen previously.
Kudos
сколько стоит купить диплом экономиста diplomdarom.ru .
See What Accident Attorneys Near Me Tricks The Celebs Are Using
accident attorneys near me [Jett]
Is Technology Making Pragmatic Kr Better Or Worse? 무료슬롯 프라그마틱
Доступ к вашему любимому букмекеру стал проще благодаря zenitbet зеркало рабочее на сегодня! С этим зеркалом вы сможете без труда продолжать делать ставки, даже если основной сайт временно недоступен. Наслаждайтесь беспрепятственным доступом к спортивным событиям и выигрышам в любое время суток!
Как избежать рисков при покупке диплома колледжа или ПТУ в России
It’s an awesome piece of writing in support of all the web people; they
will get advantage from it I am sure.
Why people still use to read news papers when in this technological globe all is accessible on web?
I don’t even know how I ended up here, but I thought this post was great.
I do not know who you are but definitely you’re going to a famous
blogger if you aren’t already 😉 Cheers!
даркнет купить диплом diplomdarom.ru .
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you! By the way, how could we communicate?
Как оказалось, купить диплом кандидата наук не так уж и сложно
Medicine prescribing information. What side effects?
buy cheap zyban for sale
All what you want to know about drugs. Read information now.
Ahooj jen jsem νám chtělrychle ɗát vědět.
Text ve vašem příspěvku sе zdají Ьěžet mimo obrazovku ѵ Firefoxu.
Nejem ѕi jistý, zda ϳе to problém s fοrmátováním nebo něco ѕ kompatibilitou internetovéһo
prohlížeče, ale myslel jsem, že vám too dám vědět,abyste ᴠěděli.
Hi, I do think this is an excellent website. I stumbledupon it 😉 I’m going to return once again since i have
saved as a favorite it. Money and freedom is the greatest way to change, may you be rich and continue to help others.
15 Wall Mounted Fireplace Benefits You Should All Be Able To On Wall Fireplace
Howdy! I know this is kinda off topic however , I’d figured I’d ask.
Would you be interested in exchanging links or maybe guest writing
a blog article or vice-versa? My site addresses a lot of the same topics as yours
and I think we could greatly benefit from each other. If you’re
interested feel free to shoot me an e-mail. I look forward to hearing from you!
Terrific blog by the way!
Стоимость дипломов высшего и среднего образования и как избежать подделок
I really like your blog.. very nice colors & theme. Did
you design this website yourself or did you hire someone to do it for you?
Plz answer back as I’m looking to create my own blog and would like to find out where u got this from.
kudos
Neat blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple adjustements would really make my blog shine.
Please let me know where you got your design. Thank you
Free Download the best premium WordPress Premium Plugin Free Download Themes, Plugins, and templates with plan. 100 original with GPL Licensed & free update.
I have assiduously enjoyed the observations provided in this reflective blog discourse.
The penman has eloquently expressed several critical themes
that align with me deeply .
As an fervent advocate of cutting-edge organizational undertakings , I would
intend to propose an entreaty to you to discover the
exceptional opportunities available at Pragmatic Play.
This energetic firm is at the leading edge
of revolutionary developments , offering a dynamic and meaningful milieu for aficionados who exhibit a passion for
eminence and a determination to advance the boundaries of what is
attainable .
I enjoin you to ponder this proposal and investigate
the bounty of possibilities that lie in store .
Respectfully be at liberty to communicate if
you have any queries or would yearn to ponder over in additional detail .
Warmest salutations ,
Here is my web blog; gambling streaming
Pills information. What side effects can this medication cause?
buy cheap zithromax without a prescription
Actual information about drug. Get here.