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
Good info. Lucky me I discovered your site by
chance (stumbleupon). I’ve book marked it for later!
I just could not go away your website before suggesting that I actually loved
the usual information a person provide to your visitors?
Is going to be back frequently to check out
new posts
I needed to thank you for this good read!! I definitely
loved every little bit of it. I’ve got you saved as a favorite to look at new things you post…
I read this paragraph fully concerning the comparison of latest and preceding technologies, it’s remarkable article.
Hi there great website! Does running a blog similar to this take a massive amount work?
I’ve absolutely no expertise in coding however I was
hoping to start my own blog in the near future. Anyhow, should you have any recommendations
or tips for new blog owners please share. I know this is off topic nevertheless I
simply wanted to ask. Thanks a lot!
It’s very trouble-free to find out any topic on net as compared to textbooks, as
I found this post at this website.
I’d like to thank you for the efforts you’ve put
in penning this site. I am hoping to see the same high-grade blog
posts by you later on as well. In fact, your creative
writing abilities has encouraged me to get my own, personal website now 😉
Hi! Someone in my Facebook group shared this website with us so I came to give
it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers!
Outstanding blog and superb design and style.
Hello mates, its great paragraph regarding educationand entirely explained, keep it up all the time.
Hi there this is kinda of off topic but I was
wanting to know if blogs use WYSIWYG editors or
if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience.
Any help would be greatly appreciated!
Someone essentially help to make seriously posts I’d state.
That is the very first time I frequented your web page and
up to now? I amazed with the analysis you made to make this actual submit
incredible. Wonderful process!
WOW just what I was searching for. Came here by searching for slot demo pragmatic rupiah
Good day! This post could not be written any better!
Reading through this post reminds me of my good old room mate!
He always kept talking about this. I will forward this write-up
to him. Fairly certain he will have a good read.
Many thanks for sharing!
It’s difficult to find experienced people in this particular subject, but you seem like you know what
you’re talking about! Thanks
Fantastic post however , I was wondering if you
could write a litte more on this subject? I’d be very thankful if
you could elaborate a little bit more. Appreciate it!
whoah this blog is fantastic i love reading your
articles. Keep up the good work! You know, lots of persons are searching round for this info, you could aid them greatly.
I am really impressed together with your writing abilities and also with the format to your weblog.
Is that this a paid subject or did you customize it your self?
Either way stay up the nice high quality writing, it is uncommon to peer a great weblog like this one these days..
I would like to thank you for the efforts you have
put in penning this site. I really hope
to view the same high-grade content by you in the future as
well. In truth, your creative writing abilities has motivated me to
get my own website now 😉
What’s up, I wish for to subscribe for this website to obtain most
up-to-date updates, so where can i do it please assist.
What a material of un-ambiguity and preserveness of valuable knowledge concerning unexpected emotions.
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you! However, how can we communicate?
Hey I know this is off topic but I was wondering if
you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time and was hoping
maybe you would have some experience with something like this.
Please let me know if you run into anything.
I truly enjoy reading your blog and I look forward
to your new updates.
If you are going for best contents like me, just pay a visit this site everyday as it provides feature contents, thanks
Hey there, You’ve done a fantastic job. I will definitely digg it
and personally suggest to my friends. I am confident they will be
benefited from this site.
Why visitors still make use of to read news papers when in this technological globe the whole thing is available on net?
Appreciating the time and energy you put into your site and
in depth information you provide. It’s great to come across a blog every once in a while that isn’t the same old rehashed information.
Great read! I’ve saved your site and I’m including your RSS feeds to my Google account.
Hi there! This blog post could not be written much better!
Looking at this article reminds me of my previous roommate!
He always kept talking about this. I am going to send this
information to him. Pretty sure he’s going to have a very good read.
Thanks for sharing!
I visited many websites except the audio quality
for audio songs current at this site is truly fabulous.
Hey there! I know this is kind of off-topic but I needed to ask.
Does running a well-established website such as
yours require a massive amount work? I’m completely
new to blogging but I do write in my journal everyday.
I’d like to start a blog so I will be able to share my experience
and views online. Please let me know if you have any suggestions or tips for brand new aspiring
bloggers. Thankyou!
I know this if off topic but I’m looking into starting my
own blog and was curious what all is needed to get
setup? I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web smart so I’m not 100% sure.
Any suggestions or advice would be greatly appreciated.
Many thanks