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
Покер-рум Pokerdom начал свою работу в 2014 году, основная направленность – страны бывшего СНГ.
pharmacy website india buy medicines from India reputable indian pharmacies
https://mexicoph24.life/# reputable mexican pharmacies online
You’ll Never Guess This Renault Kadjar Replacement Key’s
Tricks Replace
With havin so much written content do you ever run into any
issues of plagorism or copyright infringement? My blog has a lot of unique content I’ve either
authored myself or outsourced but it appears a lot of it is popping it
up all over the web without my permission. Do you know any techniques
to help protect against content from being
ripped off? I’d genuinely appreciate it.
online shopping pharmacy india indian pharmacy indianpharmacy com
Porsche Key Shell Tools To Make Your Daily Lifethe One Porsche Key Shell
Trick That Every Person Must Be Able To Porsche Key Shell
canadian pharmacy: canadian pharmacies – legal canadian pharmacy online
Five Killer Quora Answers On Secondary Double Glazing Near
Me Double glazing near Me
india pharmacy mail order http://indiaph24.store/# buy prescription drugs from india
india online pharmacy
http://indiaph24.store/# indian pharmacy online
You’ll Never Guess This Double Glazing Repair Leeds’s Secrets Double glazed Window repairs
– yogicentral.science –
A Brief History History Of Repairing Window Double glazed window repairs near me
Aw, this was a very good post. Taking the time and actual effort to generate a really good article… but what can I say… I procrastinate a whole lot
and don’t manage to get nearly anything done.
The 10 Most Scariest Things About Leeds Double Glazing Leeds Double Glazing
Guide To Auto Key Locksmith: The Intermediate Guide
To Auto Key Locksmith Auto Key locksmith
http://mexicoph24.life/# buying from online mexican pharmacy
Good day very nice web site!! Man .. Excellent ..
Amazing .. I will bookmark your blog and take the feeds also?
I am glad to search out numerous helpful info here within the post, we need develop more
techniques on this regard, thank you for sharing.
. . . . .
Buzzwords De-Buzzed: 10 Different Ways Of Saying Buy Mobility Scooter Near Me best foldable mobility scooter uk
Why Double Glazing Companies Near Me Is Relevant 2023 Replacing Double Glazed Windows
https://indiaph24.store/# world pharmacy india
Why CS GO Weapon Case Will Be Your Next Big Obsession Esports 2013 winter case
At this moment I am ready to do my breakfast, when having
my breakfast coming yet again to read additional news.
http://indiaph24.store/# pharmacy website india
world pharmacy india https://indiaph24.store/# buy medicines online in india
top online pharmacy india
Toyota Highlander 4-cylinder and Hybrid: Test Hybrid averaged 26.3 mpg
in a mixture of city and highway driving. Those texting while driving are six instances
more more likely to be in an accident than these driving drunk.
Cell phones are fantastic devices that keep us in touch with
our mates and cherished ones throughout the day, but there
are times when the phone must be put away. Though there is a class
of automotive drivers who swear by them, the handbook transmission has seen better days.
It’s not one-of-a-sort, although, since there is also a spare.
Not all of these innovations were optimistic, although, with a number of situations of
death and tragedy. It is a prison warden, though, who speaks the famous line.
Feasibility The line Bisection Test takes solely 5 minutes
to finish and is easy to score. U.S. News 360
Evaluations takes an unbiased method to our suggestions. It’s the first import to sell 1 million items in the U.S.
The Chevy Impala set the bar for automobile gross sales in 1965 when 1,074,925 items of
the Impala and Impala SS were bought.
Starter Kit Avon Explained In Fewer Than 140 Characters avon starter Kit
online canadian pharmacy reviews Large Selection of Medications from Canada canadian pharmacy in canada
5 Killer Quora Answers On Double Glazed Window Repairs Near Me Double glazed window repairs near me
mexican drugstore online: Online Pharmacies in Mexico – medication from mexico pharmacy