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
Let me reveal you in my testimonial of NewULife.
My web site ai detector turnitin check
Hello new кракен сайт маркетплейс
darknet links https://github.com/tormarkets2025ukaz1/tormarkets2025 – best darknet markets
Way cool! Some very valid points! I appreciate you writing this post and the rest
of the website is very good.
Review my web site sexual assault lawyers philadelphia
XYGENYX, a licensing firm ai detector turnitin for students
FDA-registered items.
Land 3 perk symbols to turn on the Pot Incentive
Video Game.
Feel free to surf to my web blog: online slots nz and online pokies nz
ZenBusiness supplies the best membership options.
Feel free to visit my web-site: how to start an llc in virginia
Real HGH Perks (NewULife Hgh Gel Not Tested Yet).
Visit my blog ai detector free grammarly
how to buy generic nexium without insurance
A person necessarily assist to make critically posts I’d state.
This is the very first time I frequented your website page and thus far?
I surprised with the research you made to create this particular
publish incredible. Wonderful job!
Review my web-site; sexual assault lawyers philadelphia
The best cbd drops for cats products are made from
top notch hemp CBD.
dublin seo agency https://seo-agency-1.com/
Just what I was looking for, appreciate it for posting.
my blog post; sexual misconduct lawyers near Me
I love it when folks come together and share thoughts. Great blog, continue
the good work!
Also visit my homepage … sexual assault defense lawyers near me
If you are going for best contents like I do, simply go to see this web
page every day since it presents feature contents, thanks
Hello new как пополнить сайт кракен kraken one com
NewULife Ranks. Welcome to my NewUlife Testimonial.
Here is my blog post: heavy duty truck parts texas
Let me reveal you in my review of NewULife.
Check out my web blog; ai detector free
darknet marketplace https://github.com/darknetwebsitesgflpx/darknetwebsites – onion dark website
NewULife Ranks. Invite to my NewUlife Review.
Here is my web page: ai detector humanize
Yes, Aviva mortgages over 70 are 1.97% APRC.
Here is my blog … retirement interest-only mortgage rates uk
No infrared sauna room ng202-hce timber lasts longer
than cedarwood.
muslim seo agency https://seo-agency-1.com/
An artificial USP human development hormone (somatropin).
Feel free to visit my site; ai detector free online
dark web markets https://github.com/darkmarketlinkp22jr/darkmarketlink – darknet markets onion address
Drugs information leaflet. What side effects can this medication cause?
propranolol high pulse
Some trends of drugs. Read now.
darkmarkets https://github.com/darkmarketlinkp22jr/darkmarketlink – bitcoin dark web
ОНЛАЙН КАЗИНО
http://ionpron.net/profile.php?mode=viewprofile&u=1620478
dark web drug marketplace https://github.com/darknetdruglinksvojns/darknetdruglinks – dark market link
darkmarket https://github.com/darknetdruglinksvojns/darknetdruglinks – darknet markets 2025