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
Hi! I’m at work browsing your blog from my new iphone 4!
Just wanted to say I love reading your blog and
look forward to all your posts! Carry on the
superb work!
Wow! After all I got a weblog from where I can truly obtain helpful data concerning my study
and knowledge.
Hmm is anyone else encountering problems with the images on this blog loading?
I’m trying to figure out if its a problem on my end or if it’s the blog.
Any feedback would be greatly appreciated.
I like the valuable info you provide in your articles.
I’ll bookmark your blog and check again here frequently.
I am quite sure I’ll learn lots of new stuff right here!
Good luck for the next!
Hi, I do think this is a great site. I stumbledupon it 😉 I may return once again since I saved as a favorite it.
Money and freedom is the best way to change, may you be rich and continue
to help others.
I do not even know how I ended up here, but I thought this post was good.
I do not know who you are but certainly you are going to a famous blogger if you aren’t already 😉 Cheers!
Greetings! I know this is kinda off topic nevertheless I’d figured I’d ask.
Would you be interested in trading links or maybe guest writing a blog
post or vice-versa? My site addresses a lot of the same subjects as yours and I believe we
could greatly benefit from each other. If you
might be interested feel free to send me an email.
I look forward to hearing from you! Wonderful blog by the way!
Good day! Do you know if they make any plugins to safeguard against hackers?
I’m kinda paranoid about losing everything I’ve worked hard
on. Any recommendations?
Greetings, There’s no doubt that your blog may be having web browser
compatibility issues. Whenever I take a look at your site in Safari, it
looks fine however, if opening in Internet Explorer, it’s got some overlapping issues.
I simply wanted to give you a quick heads up! Besides that,
fantastic site!
You should take part in a contest for one of the most useful sites on the internet.
I’m going to recommend this site!
Admiring the persistence you put into your website and in depth information you present.
It’s good to come across a blog every once in a while that isn’t the same out of date rehashed material.
Wonderful read! I’ve saved your site and I’m including your RSS feeds to my
Google account.
I visited various sites except the audio quality for audio songs present at this web page is in fact excellent.
Excellent website you have here but I was curious if you knew of any user discussion forums that
cover the same topics talked about in this article? I’d really love to be a part of online community where I can get opinions from other
knowledgeable people that share the same interest.
If you have any recommendations, please let
me know. Thanks a lot!
Greetings! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the same niche.
Your blog provided us valuable information to work on. You have done a
outstanding job!
I’m not sure exactly why but this weblog is loading incredibly slow for me.
Is anyone else having this issue or is it a issue on my end?
I’ll check back later and see if the problem still exists.
Hello, everything is going perfectly here and ofcourse every one is sharing data,
that’s in fact fine, keep up writing.
I used to be suggested this website by way of my cousin. I’m now not sure whether or not this publish is written through him as nobody else know such detailed
about my trouble. You are wonderful! Thanks!
Howdy! I could have sworn I’ve been to this web
site before but after browsing through some of the articles I realized it’s
new to me. Anyhow, I’m certainly happy I stumbled upon it and I’ll be bookmarking it and checking back often!
you are actually a excellent webmaster. The site loading
speed is incredible. It sort of feels that you’re doing any distinctive
trick. Furthermore, The contents are masterpiece.
you’ve performed a excellent activity on this topic!
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
You definitely know what youre talking about, why waste your intelligence on just posting videos to
your weblog when you could be giving us something enlightening
to read?
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!) Wonderful job. I really enjoyed what you had to say, and more than that, how you presented it.
Too cool!
Tremendous things here. I’m very glad to look your article.
Thank you so much and I am looking ahead to contact you.
Will you kindly drop me a e-mail?
I relish, lead to I discovered just what I was having a look for.
You’ve ended my 4 day lengthy hunt! God Bless you
man. Have a nice day. Bye
We’re a gaggle of volunteers and opening a brand new scheme in our community.
Your website provided us with helpful info to paintings
on. You’ve done an impressive job and our entire community will likely be grateful to you.
It’s going to be finish of mine day, but before end I am reading this enormous paragraph to improve my know-how.
Hi, i think that i saw you visited my weblog thus i came to go back the prefer?.I’m attempting to to find issues to improve my website!I
suppose its ok to use some of your concepts!!
Wir lieben unsere Fahrräder und E-Bikes. Sehen und gesehen werden kann in der Dunkelheit überlebenswichtig
sein. Daher kann eine gute Fahrradlampe zur Überlebensversicherung werden. Aber in der
dunklen Jahreszeit kann das Fahrradfahren in den städten zu einem echten Sicherheitsrisiko werden.
Grund genug für uns aktuelle Modelle der
Fahrradbeleuchtung genau zu testen. Welche Fahrradbeleuchtung haben wir getestet?
Es gibt uns daher die Testergebnisse von ADAC, Stiftung Warentest angesehen und auch nochmal die beliebtesten Produkte bei Amazon recherchiert.
Leider verzichten insbesondere Hersteller sportlich ausgelegter Fahrräder bei der Zusammenstellung der Komponenten zu oft auf hochwertige Fahrradlampen. Herausgekommen ist eine Top 5 der besten LED Fahrradlichter zum Anstecken. Im nächsten Schritt haben wir diese Top 5 der besten Fahrradlampen selbst auf Herz und Nieren getestet um zu prüfen, ob die Herstellerangaben und Testergebnisse dauerhaft.
Das Herzstück in unserem Fahrradbeleuchtung Test war die Ausleuchtung und der Leuchtkegel der getesteten Fahrradlampen. Es gibt alle
Fahrradlampen im Stadtverkehr, als auch auf unbeleuchteten Straßen und dunklen Waldwegen ausprobiert.
Thankfulness to my father who shared with me on the topic of this website, this webpage is actually
awesome.
Valuable info. Lucky me I discovered your website unintentionally, and I am stunned why this accident did not
happened in advance! I bookmarked it.
Hello outstanding blog! Does running a blog similar to this require a large amount
of work? I’ve virtually no understanding of coding but I had been hoping to
start my own blog soon. Anyway, should you have any ideas or techniques
for new blog owners please share. I know this is off topic but I
simply needed to ask. Thanks!