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
http://www.demafahuinam.org/
Here is my website :: Explore Daycares Locations
That is very attention-grabbing, You are a very skilled blogger.
I’ve joined your feed and look ahead to looking for more of your fantastic post.
Also, I have shared your website in my social networks
I’m impressed, I must say. Rarely do I come across a blog that’s both educative and interesting, and let me tell you, you have
hit the nail on the head. The issue is something not enough
men and women are speaking intelligently about.
I am very happy I came across this in my search for something concerning this.
unlike this version of poker, the seven-card stud originated in https://sugarpromocodes.com/ only in the middle 19th century,
and was widely distributed by the American military.
Yesterday, while I was at work, my cousin stole my iphone
and tested to see if it can survive a 25 foot drop, just so she can be
a youtube sensation. My iPad is now broken and she has 83
views. I know this is completely off topic but I
had to share it with someone!
http://slotsiteleri.bid/# slot oyunlar? siteleri
Way cool! Some very valid points! I appreciate you penning this write-up plus the rest of the site is also very good.
You should take part in a contest for one of the highest
quality websites on the internet. I am going to recommend this
website!
http://slotsiteleri.bid/# en iyi slot siteleri 2024
Парадокс, но купить диплом кандидата наук оказалось не так и сложно
lunacharskiy.com/images/pages/?kak_bezopasno_i_legalno_kupit_diplom_v_rf
Awesome post.
deneme bonusu veren slot siteleri: deneme veren slot siteleri – slot kumar siteleri
https://denemebonusuverensiteler.win/# bahis siteleri
I am sure this piece of writing has touched all the internet visitors,
its really really nice piece of writing on building up new webpage.
Thiѕ post іs reallʏ a good onee it assists neᴡ the web
viewers, wһo are wishing іn favor of blogging.
Also visit my web site situs toto
deneme bonusu veren siteler: en yeni slot siteleri – yasal slot siteleri
Way cool! Some extremely valid points! I appreciate you writing this article and also the rest of
the website is extremely good.
Also visit my web blog :: https://www.cucumber7.com/
Hey There. I found your blog the use of msn. That is a
very well written article. I will make sure to bookmark it and return to read more
of your helpful information. Thank you for the post. I
will certainly comeback.
Hi there, all the time i used to check blog posts here in the early
hours in the morning, as i love to gain knowledge of more and more.
Have a look at my website: is pro nerve 6 a scam
https://sweetbonanza.network/# sweet bonanza hilesi
I have to thank you for the efforts you have put in writing this blog.
I’m hoping to see the same high-grade content by you in the future as well.
In truth, your creative writing abilities has inspired me to get my very
own website now 😉
Профессиональный сервисный центр по ремонту холодильников и морозильных камер.
Мы предлагаем: ремонт холодильников с выездом
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
oyun siteleri slot: en iyi slot siteler – bonus veren slot siteleri
What a material of un-ambiguity and preserveness of precious experience on the topic of unpredicted emotions.
Titleist Clubs – Good Or Bad? 부산오피, http://mckenneyssux.com/__media__/js/netsoltrademark.php?d=rentry.co/mzvb53pd,
guvenilir slot siteleri: en guvenilir slot siteleri – slot siteleri
Процесс получения диплома стоматолога: реально ли это сделать быстро?
Приобретение школьного аттестата с официальным упрощенным обучением в Москве
I was wondering if you ever thought of changing the layout of your
blog? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people could
connect with it better. Youve got an awful lot
of text for only having 1 or 2 images. Maybe
you could space it out better?