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
how to buy cheap clomid without prescription: rex pharm – where to get clomid without dr prescription
innocently,セックス ロボットmay be driving their unorthodox imaginings.
I have read so many articles concerning the blogger lovers but this piece of writing is really a good piece of writing,
keep it up.
generic clomid without insurance: rex pharm – how to get cheap clomid online
priligy maxpharm: buy priligy – buy priligy
Hi, yes this article is in fact pleasant and I have learned lot of things from it regarding blogging.
thanks.
amoxicillin capsule 500mg price: cheap amoxil – amoxicillin 500mg buy online uk
I just could not go away your web site before suggesting that I actually enjoyed
the usual info a person provide in your visitors? Is gonna be back ceaselessly in order to investigate cross-check new posts
Купить диплом ВУЗа России
Medication information. What side effects can this medication cause?
can you buy cheap levaquin price
All trends of pills. Get information now.
Купить диплом в России, предлагает наша компания
200 mg prednisone daily: cheap prednisone – prednisone
Купить диплом ВУЗа России
Heya i’m for the primary time here. I found this
board and I find It truly helpful & it helped me out a lot.
I hope to present something back and aid others such as you helped me.
Good way of telling, and good paragraph to
get data concerning my presentation topic, which i
am going to deliver in academy.
Hmm it looks like your blog ate my first comment (it was super
long) so I guess I’ll just sum it up what I submitted and
say, I’m thoroughly enjoying your blog. I too am an aspiring blog blogger but I’m still new to the whole thing.
Do you have any helpful hints for novice blog writers?
I’d genuinely appreciate it.
can i buy generic clomid: rex pharm – clomid without a prescription
купить кухню в екатеринбурге — Легко и удобно приобрести кухню в Екатеринбурге.
how to buy clomid without a prescription: clomid – buying generic clomid without dr prescription
how can i get cheap clomid online: clomid – can you get clomid online
Fachai Casino – best slot online in the Philippines
better than jili slot and pg slot
free Spins 3000+ Free Demo Slot
searching fachaicasino.org on Google or Bing to get free coins for play
https://fachaicasino.org/
В неожиданном повороте событий мемкоины вытеснили биткоин из топа самых обсуждаемых криптоактивов в социальных сетях. По данным аналитической платформы Santiment, такие токены как PNUT, PEPE и DOGE заняли лидирующие позиции, оттеснив первую криптовалюту на седьмое место по популярности меметокен тренд
Hmm is anyone else having problems with the images on this blog loading?
I’m trying to determine if its a problem on my end
or if it’s the blog. Any feedback would be greatly appreciated.
buy priligy: dapoxetine online – buy priligy
dark web links dark market 2024 https://darknetmarketstore.com/ – darkmarket list
dapoxetine price: priligy maxpharm – priligy
I got this website from my friend who informed me
regarding this web site and now this time I am browsing this website and reading very informative articles at this
place.
Hey there! I understand this is somewhat off-topic however I needed to
ask. Does managing a well-established website such as yours take
a large amount of work? I’m completely new to operating a blog but I do write in my
journal every day. I’d like to start a blog so I can easily share my experience
and views online. Please let me know if you have any kind of suggestions or tips for
new aspiring bloggers. Appreciate it!
dapoxetine price: dapoxetine online – dapoxetine online
Hello, I log on to your blog like every week. Your story-telling style is witty, keep up the good
work!