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
10 Healthy Adhd Psychiatry Near Me Habits best Adhd Psychiatrist near me
https://ozempic.art/# ozempic generic
Excellent web site you have got here.. It’s hard to
find quality writing like yours these days. I truly appreciate people like you!
Take care!!
How Handles And Hinges Became The Hottest Trend Of 2024 glass window accessories friction stay hinge [Adolfo]
Great blog here! Also your website loads up very fast!
What host are you using? Can I get your affiliate
link to your host? I wish my website loaded up as fast as yours lol
5 Must-Know Hismphash Practices You Need To Know For 2023 private adhd assessment bristol
Do You Think Sport Toto Result Today One Day Rule The World?
먹튀검증
http://rybelsus.shop/# rybelsus cost
buy ozempic: ozempic cost – buy ozempic
You’ll Never Guess This Upvc Door Doctor Near Me’s Secrets door doctor Near me (ai-db.science)
ozempic: ozempic cost – ozempic coupon
The last-observation-carried-forward method was used to estimate missing efficiency
information.
buy ozempic: buy ozempic pills online – buy cheap ozempic
https://rybelsus.shop/# semaglutide tablets
Just want to say your article is as surprising. The clarity for your post is just excellent and that i could think you’re a professional
on this subject. Fine together with your permission let
me to grab your RSS feed to keep up to date with
coming near near post. Thanks one million and please carry on the rewarding work.
cheapest rybelsus pills semaglutide cost buy semaglutide online
Genuinely when someone doesn’t know afterward its up
to other users that they will assist, so here it happens.
For these factors, any type of off-label or nonprescription use of GHSs is not secure.
https://ozempic.art/# Ozempic without insurance
We are a group of volunteers and opening a new scheme in our community.
Your web site offered us with valuable info to work on. You’ve done a formidable job and our whole community will be thankful to
you.
Have a look at my web blog :: Inviertas
This paragraph is in fact a nice one it helps new the web viewers,
who are wishing in favor of blogging.
5 Killer Quora Answers On Replacement Key For Audi
A3 replacement key for audi A3
https://rybelsus.shop/# cheapest rybelsus pills
semaglutide tablets: buy semaglutide pills – semaglutide tablets
Hi mates, fastidious post and fastidious urging commented at this place,
I am genuinely enjoying by these.
buy cheap ozempic ozempic cost buy ozempic pills online
Здравствуйте!
Мы предлагаем дипломы.
msobl.flybb.ru/posting.php?mode=post&f=2
Thank you a lot for sharing this with all folks you actually recognise what you
are talking approximately! Bookmarked. Please additionally consult with my website =).
We can have a hyperlink trade arrangement among us
Review my website – Мега магазин
Wow that was strange. I just wrote an extremely long comment but after I clicked submit my comment
didn’t show up. Grrrr… well I’m not writing all that over again. Anyway, just wanted to say superb
blog!
A How-To Guide For Private Adult ADHD Diagnosis From Beginning To End private adhd assessment guildford (https://psychiatry25493.wikiexcerpt.com/3023589/20_resources_that_will_make_you_more_successful_at_adhd_private_assesment)