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
tamoxifen buy: tamoxifen and antidepressants – tamoxifen endometriosis
https://ciprofloxacin.tech/# ciprofloxacin mail online
http://cytotec.club/# order cytotec online
Hey very interesting blog!
Here is my blog post; cancer research
cost propecia for sale: propecia price – cost generic propecia without prescription
https://cytotec.club/# order cytotec online
Вы можете использовать традиционные методы, такие как электронные
кошельки, кредитные карты и банковские переводы для осуществления платежей.
https://finasteride.store/# get propecia tablets
cost cheap propecia without prescription cost propecia without prescription cost generic propecia pill
5 Killer Qora’s Answers To Double Glazed Windows Leeds Upvc door Repairs Meanwood
http://cytotec.club/# buy cytotec pills online cheap
7 Tricks To Help Make The Most Out Of Your Slot Features progressive slots,
Maura,
https://finasteride.store/# buying propecia price
http://ciprofloxacin.tech/# antibiotics cipro
http://finasteride.store/# buy propecia pills
Undeniably consider that that you stated. Your favorite
justification seemed to be on the internet the simplest thing to understand
of. I say to you, I certainly get irked while other people consider worries that they
plainly do not recognize about. You managed How to interpret forex signals hit the nail upon the top and also defined out
the whole thing without having side effect ,
folks could take a signal. Will probably be again to get more.
Thanks
Heya i’m for the first time here. I came across this board and I
find It truly useful & it helped me out much. I hope to
give something back and aid others like you aided me.
http://finasteride.store/# cost of propecia online
Hi there, just became aware of your blog through Google, and found that it
is truly informative. I am gonna watch out for
brussels. I’ll be grateful if you continue this in future.
Numerous people will be benefited from your writing. Cheers!
http://lisinopril.network/# cost of lisinopril in canada
order cytotec online buy misoprostol over the counter buy cytotec pills
I don’t know what were the codes for. But I truly appreciate the effort you exert to your blog.
15 Documentaries That Are Best About Window Companies Leeds door repairs leeds (Janell)
http://ciprofloxacin.tech/# ciprofloxacin 500mg buy online
cytotec pills buy online buy cytotec in usa buy cytotec pills online cheap
nolvadex gynecomastia: tamoxifen dose – tamoxifen and grapefruit
A Guide To Glass Repair Leeds From Start To
Finish double glazing repair (Santo)
Hi would you mind letting me know which web host you’re working with?
I’ve loaded your blog in 3 completely different browsers and I must
say this blog loads a lot faster then most. Can you recommend a good hosting provider at a honest price?
Thanks, I appreciate it!
Three Wheel Mobility Scooter’s History Of Three Wheel Mobility Scooter In 10 Milestones 3 wheeled mobility Scooters
chandal juventus 2023