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
This design is incredible! You definitely know how to keep a reader amused.
Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great
job. I really loved what you had to say, and more than that, how you presented it.
Too cool!
Everything is very open with a clear description of the challenges.
It was definitely informative. Your website is very helpful.
Many thanks for sharing!
WOW just what I was looking for. Came here by searching for play demo habanero
Quality articles or reviews is the important to be a focus
for the viewers to go to see the web site, that’s what this site is providing.
Today, I went to the beachfront with my children. I found a sea shell and gave it to
my 4 year old daughter and said “You can hear the ocean if you put this to your ear.”
She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is completely off topic but I had to tell someone!
I was curious if you ever considered changing the layout of your site?
Its very well written; I love what youve got to say. But maybe you could a
little more in the way of content so people could connect with it better.
Youve got an awful lot of text for only having one
or 2 images. Maybe you could space it out better?
When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is
added I get four emails with the same comment.
Is there any way you can remove me from that service? Bless you!
I like what you guys are usually up too. This sort of clever work and coverage!
Keep up the excellent works guys I’ve included you guys to
my blogroll.
Hi! I know this is sort of off-topic however I needed to ask.
Does building a well-established blog like yours take a lot of work?
I am brand new to writing a blog however I do write in my diary everyday.
I’d like to start a blog so I will be able to
share my own experience and views online. Please let me know if you have any ideas or tips for brand new aspiring bloggers.
Appreciate it!
Hey there! I realize this is somewhat off-topic however I needed to ask.
Does building a well-established blog such as yours require a lot of work?
I’m completely new to blogging but I do write in my
diary every day. I’d like to start a blog so I can share
my personal experience and feelings online. Please let me know if you have any kind of ideas or tips for brand new aspiring bloggers.
Appreciate it!
I am sure this article has touched all the internet users, its really really
fastidious piece of writing on building up new weblog.
I am really enjoying the theme/design of your website.
Do you ever run into any internet browser compatibility
problems? A couple of my blog readers have complained about my site
not operating correctly in Explorer but looks great in Safari.
Do you have any recommendations to help fix this issue?
you’re truly a good webmaster. The site loading speed is
incredible. It sort of feels that you’re doing
any unique trick. Also, The contents are masterwork. you’ve done a fantastic
job on this matter!
We are a group of volunteers and starting a new scheme in our community.
Your web site provided us with valuable info to work on. You have
done a formidable job and our entire community will be thankful to you.
After looking over a handful of the blog articles on your site, I truly appreciate your way of writing a blog.
I bookmarked it to my bookmark website list
and will be checking back soon. Take a look at my website as well and tell me what
you think.
Aw, this was an exceptionally good post. Taking a few
minutes and actual effort to generate a really good article… but what can I say… I hesitate a whole
lot and never seem to get anything done.
I have been browsing online more than 2 hours today, yet I never found
any interesting article like yours. It is pretty worth
enough for me. Personally, if all webmasters and bloggers made good
content as you did, the internet will be much more useful than ever before.
Have you ever thought about creating an e-book or guest authoring on other blogs?
I have a blog centered on the same ideas you discuss and would love
to have you share some stories/information. I know my audience
would appreciate your work. If you are even remotely interested, feel
free to send me an e mail.
I know this if off topic but I’m looking into starting my own blog
and was curious what all is required to get set up? I’m assuming having a blog like
yours would cost a pretty penny? I’m not very web savvy so I’m
not 100% positive. Any suggestions or advice would be greatly appreciated.
Appreciate it
It’s fantastic that you are getting ideas from this article as well
as from our dialogue made here.
Great beat ! I wish to apprentice while you amend your website, how could i subscribe for a blog website?
The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea
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 blog when you could be giving us something
enlightening to read?
Its like you read my mind! You seem to know so much about this, such as
you wrote the guide in it or something. I believe that you
simply could do with some percent to force the message house a little bit, however instead
of that, this is excellent blog. A fantastic read.
I will certainly be back.
Its like you read my mind! You seem to know a lot about this, like
you wrote the book in it or something. I think that you can do with a few
pics to drive the message home a little bit, but instead of
that, this is fantastic blog. A great read.
I’ll certainly be back.
This is my first time go to see at here and i am really impressed
to read all at alone place.
My brother recommended I might like this blog.
He was entirely right. This post actually made
my day. You cann’t imagine simply how much time I had spent
for this information! Thanks!
My brother recommended I might like this web site. He was entirely right.
This submit truly made my day. You cann’t consider just how a lot
time I had spent for this info! Thanks!
I’ve been surfing on-line greater than three hours today, yet I by no means discovered any
attention-grabbing article like yours. It is beautiful worth sufficient for me.
Personally, if all site owners and bloggers made good content as you
probably did, the web will likely be much more helpful than ever before.
It’s appropriate time to make some plans for the future and it’s time to be
happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips.
Maybe you can write next articles referring to this article.
I want to read more things about it!
Hello would you mind stating which blog platform you’re using?
I’m going to start my own blog in the near future but
I’m having a tough time choosing between BlogEngine/Wordpress/B2evolution and
Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something
unique. P.S My apologies for getting off-topic but I had to ask!