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
Hey there excellent website! Does running a blog like this
take a massive amount work? I have very little understanding of coding however I had been hoping to start my
own blog soon. Anyways, if you have any recommendations or tips
for new blog owners please share. I know this is off subject however I simply wanted to
ask. Many thanks!
If you are going for most excellent contents like myself, simply pay a visit this site daily for the reason that it gives feature contents, thanks
Its like you read my mind! You appear to know so much about this, like
you wrote the book in it or something. I think that you could do with a
few pics to drive the message home a bit,
but other than that, this is fantastic blog. A fantastic read.
I’ll certainly be back.
whoah this blog is great i like reading your posts. Stay
up the good work! You understand, many people are hunting around for this info, you could aid them
greatly.
Yes! Finally someone writes about repo.getmonero.org.
This design is spectacular! You obviously know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to
start my own blog (well, almost…HaHa!) Excellent job.
I really enjoyed what you had to say, and more than that,
how you presented it. Too cool!
Hey There. I found your blog using msn. This is an extremely well written article.
I’ll be sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I will certainly comeback.
If some one desires expert view on the topic of running a blog then i suggest him/her to pay a visit this website, Keep up the good work.
bookmarked!!, I really like your site!
This is a topic that’s near to my heart… Cheers! Where are your contact details though?
Attractive part of content. I simply stumbled upon your blog and in accession capital to
claim that I get in fact enjoyed account your blog posts.
Any way I will be subscribing to your augment and even I achievement you get admission to persistently
quickly.
Wow, fantastic weblog format! How long have you been running a blog for?
you made blogging look easy. The full glance of your site is excellent,
as neatly as the content material!
I used to be able to find good information from your blog posts.
This paragraph will help the internet visitors for building up new webpage or even a weblog from start to end.
Very rapidly this website will be famous among all blogging and site-building visitors, due to it’s fastidious
content
A person essentially help to make critically posts I would state.
This is the very first time I frequented your website page and up to now?
I amazed with the analysis you made to make this particular submit extraordinary.
Wonderful job!
Unquestionably consider that that you stated. Your
favorite reason appeared to be at the internet the simplest thing to take note of.
I say to you, I certainly get annoyed at the same time as people think about issues that they
plainly don’t understand about. You managed to hit the nail upon the top and outlined out the entire thing with no
need side effect , other people can take a signal.
Will probably be back to get more. Thanks
Very good write-up. I definitely love this site. Stick with it!
My spouse and I stumbled over here by a different website and thought I should
check things out. I like what I see so now i’m
following you. Look forward to looking into your web page repeatedly.
I enjoy what you guys are up too. This sort of clever work and
exposure! Keep up the amazing works guys I’ve included you guys to my own blogroll.
Hello There. I found your blog using msn. This is a very well
written article. I’ll make sure to bookmark it and come back to read more
of your useful information. Thanks for the post.
I will certainly return.
Hi i am kavin, its my first time to commenting anyplace,
when i read this post i thought i could also make comment due to this brilliant paragraph.
I just could not leave your website before suggesting
that I extremely loved the standard information an individual provide for your visitors?
Is gonna be back frequently in order to check out new posts
Excellent weblog right here! Additionally your website so
much up fast! What web host are you the usage
of? Can I get your affiliate link to your host?
I desire my website loaded up as fast as yours lol
Hello, i think that i saw you visited my site thus i came to “return the favor”.I am trying to find things to improve my website!I suppose its
ok to use some of your ideas!!
I am really grateful to the holder of this site who has shared this impressive piece of writing at here.
I think that is one of the so much significant information for me.
And i’m glad studying your article. However wanna remark on few general
issues, The site taste is ideal, the articles is in point of fact
excellent : D. Just right process, cheers
Sportart hat sich die letzte zeit Jahren, auch dank moderner Materialien für die Walking Stöcke, als Trendsport etabliert.
Hiermit Einsatz von Stöcken wird zusätzlich die Muskulatur des Oberkörpers beansprucht.
Der Ausdauereffekt dieser Sportart und die Möglichkeit Nordic walking in der Natur, aber
auch in der Stadt unschwer realisieren zu können, haben hierzu beigetragen.
Sport ist für ambitionierte Sportler ebenso geeignet wie
für untrainierte Menschen. Die Gelenke werden jedoch stärker belastet als beim normalem Spazieren. 5% höher als beim konventionellen Walking,
wenn dieses ohne zusätzliche Oberkörperbewegungen durchgeführt wird.
Nach einer Studie von Sportmedizinern des Krankenhauses für Sportverletzte
in Hellersen ist der Sauerstoffverbrauch und daraus folgernd die Ausdauerbelastung beim Sport mit korrekter Ausführung ca.
Sportart ist eine Sportart mit zyklischem Bewegungsablauf,
wobei der rechte Stock immer dann Bodenberührung hat, wenn Die Linkspartei Ferse aufsetzt und der linke Stock, wenn die rechte Ferse aufsetzt.
Die Stöcke werden nah am Körper geführt.
Can I just say what a relief to uncover someone who truly understands what they’re talking about on the web.
You actually realize how to bring a problem to light and make it important.
A lot more people need to check this out
and understand this side of your story. I can’t believe you are not more popular because you definitely have the gift.
I will immediately clutch your rss feed as I
can not to find your e-mail subscription link or newsletter service.
Do you’ve any? Please allow me recognize so that I may just subscribe.
Thanks.