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
Superb blog! Do you have any hints for aspiring writers?
I’m planning to start my own site soon but I’m
a little lost on everything. Would you recommend starting with a free platform like
Wordpress or go for a paid option? There are so many options out there that I’m totally overwhelmed ..
Any tips? Kudos!
Highly energetic article, I loved that a lot.
Will there be a part 2?
I always used to study post in news papers but now as I am
a user of net so from now I am using net for articles or reviews, thanks to web.
Great post. I was checking continuously this blog and I am impressed!
Extremely useful info particularly the last part
🙂 I care for such information much. I was looking for this certain info
for a very long time. Thank you and good luck.
Woah! I’m really loving the template/theme of this site.
It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and
appearance. I must say you have done a amazing job with this.
Additionally, the blog loads super fast for me on Internet explorer.
Exceptional Blog!
Saved as a favorite, I love your website!
you are truly a excellent webmaster. The website loading speed
is amazing. It seems that you are doing any unique trick.
Moreover, The contents are masterwork. you’ve performed a
wonderful job on this subject!
Have you ever thought about adding a little
bit more than just your articles? I mean, what
you say is valuable and everything. However imagine if
you added some great visuals or video clips to give your posts
more, “pop”! Your content is excellent but with images and videos, this blog could
undeniably be one of the most beneficial in its
niche. Terrific blog!
Nice post. I was checking constantly this blog and I’m
impressed! Very helpful info particularly the last
part 🙂 I care for such info a lot. I was looking for this certain info for a long time.
Thank you and best of luck.
An interesting discussion is definitely worth comment. I
think that you ought to publish more on this issue, it might not be a taboo matter but typically people do not discuss such topics.
To the next! Cheers!!
I am sure this post has touched all the internet
viewers, its really really nice article on building up new web site.
hey there and thank you for your information – I have certainly picked up
anything new from right here. I did however expertise a few technical issues using this site,
as I experienced to reload the site many times previous to I could get it to load properly.
I had been wondering if your web hosting is OK? Not that I’m
complaining, but slow loading instances times will often affect your placement in google and can damage
your high quality score if ads and marketing with Adwords.
Anyway I’m adding this RSS to my e-mail and could look out for a lot more of your respective exciting
content. Ensure that you update this again soon.
I was recommended this web site by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem.
You are amazing! Thanks!
Quality content is the secret to be a focus for
the visitors to pay a quick visit the web site, that’s what this
website is providing.
Bei der Frage nach einem guten Bewegungsmelder kommt direkt die Frage auf: Was sagt eigentlich die Stiftung Warentest diesbezüglich?
Leider hat die Stiftung Warentest noch keinen Bewegungsmelder-Test gemacht und noch keinen Testsieger gekürt.
5.4. Bewegungsmelder- was ist Unterkriechschutz?
Aber auch in unserem Vergleich zum Thema finden Sie bestimmt einige schöne Anregungen. Mit seiner Hilfe können auch Bereiche unterm Melder,
die zuvor nicht abgedeckt wurden und als sogenannte tote Zonen galten, nichts dabei sein mit in die „Überwachung” einbezogen werden. Der Unterkriechschutz ist eine tolle und sinnvolle Neuerung. 5.5. Bewegungsmelder – wie anschließen? Welche Preisspanne deckt der Bewegungsmelder-Vergleich der VGL-Redaktion ab? Um einen guten Überblick zu garantieren, wurden hier 13 Modelle von 7 verschiedenen Herstellern für Sie zusammengestellt – so können Sie bequem Ihre Lieblingsmarke unter folgenden Marken wählen: Steinel, SEBSON, Emos, Goobay, Homematic IP, Chilitec, REV-Ritter. Von 7,87 Euro bis 59,95 Euro ist hier die Budget etwas Passendes dabei. Welches von den 13 Bewegungsmelder-Modellen aus dem Vergleich.org-Vergleich hat es gibt mehr Kundenrezensionen erhalten?
Hello there! Do you use Twitter? I’d like to follow you
if that would be okay. I’m definitely enjoying your blog and look forward to new updates.
Thank you a lot for sharing this with all of us you actually know
what you are talking approximately! Bookmarked.
Kindly additionally consult with my site =). We
could have a hyperlink change contract among us
Very nice article, totally what I wanted to find.
What’s up, after reading this awesome article i am also happy to share my knowledge here with colleagues.
This post is in fact a pleasant one it assists new internet people,
who are wishing in favor of blogging.
Hello colleagues, pleasant article and good arguments commented here,
I am in fact enjoying by these.
Hi there excellent blog! Does running a blog similar to
this take a large amount of work? I’ve virtually no understanding of
programming however I was hoping to start my own blog soon. Anyways, if you have any recommendations or techniques for new blog
owners please share. I know this is off subject but I just wanted to
ask. Thank you!
I would like to thank you for the efforts you’ve put in writing this
website. I’m hoping to see the same high-grade content from you later on as well.
In truth, your creative writing abilities has encouraged me
to get my own, personal blog now 😉
Hello there, I discovered your site by way of Google whilst searching
for a related subject, your web site got here up, it looks good.
I have bookmarked it in my google bookmarks.
I am really loving the theme/design of your blog. Do you ever run into any internet browser compatibility problems?
A few of my blog audience have complained about my blog not operating correctly in Explorer but looks great in Safari.
Do you have any suggestions to help fix this problem?
Hi there, I discovered your web site by means of Google at the same time as looking
for a related topic, your web site got here up, it looks good.
I have bookmarked it in my google bookmarks.
Hi there, just became alert to your weblog via Google, and found that it is really informative.
I am gonna watch out for brussels. I will appreciate should you continue this in future.
Numerous folks can be benefited out of your writing.
Cheers!
I’m impressed, I must say. Rarely do I encounter a blog that’s
both educative and interesting, and let me tell you, you’ve hit the nail on the head.
The issue is something that not enough folks are speaking intelligently about.
I’m very happy I came across this in my search for something relating
to this.
That is very fascinating, You are an overly skilled blogger.
I have joined your feed and sit up for in the hunt for more of your great post.
Additionally, I have shared your website in my social networks
Thanks for sharing such a good opinion, article is fastidious, thats why i have read it fully
Hello Dear, are you actually visiting this web site on a regular basis, if so after that you
will without doubt obtain pleasant experience.