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
Wonderful blog! I found it while browsing on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Many thanks
each time i used to read smaller posts that also clear their
motive, and that is also happening with this piece of writing which I am reading
here.
I’m not that much of a online reader to be honest
but your blogs really nice, keep it up! I’ll go ahead and bookmark your
website to come back in the future. Many thanks
This piece of writing will help the internet viewers for building up new blog or even a blog from start to end.
I read this paragraph fully regarding the resemblance of
most recent and previous technologies, it’s amazing article.
Hey! I know this is kind of off-topic but I had to ask. Does building a well-established website like yours take a
large amount of work? I am completely new to operating a blog but I do write in my
journal everyday. I’d like to start a blog so I
can easily share my experience and views online.
Please let me know if you have any kind of suggestions or
tips for new aspiring bloggers. Appreciate it!
I have been surfing online more than 4 hours today, yet I never
found any interesting article like yours. It’s pretty
worth enough for me. In my view, if all website owners and
bloggers made good content as you did, the net will be a lot more
useful than ever before.
Pretty! This has been an extremely wonderful post. Thank you for providing this information.
Great goods from you, man. I’ve understand your stuff previous to and you’re just too excellent.
I really like what you’ve acquired here, certainly like what you’re stating and the
way in which you say it. You make it enjoyable and you still care
for to keep it smart. I can’t wait to read much more from
you. This is actually a wonderful site.
What’s up to every , for the reason that I am genuinely keen of reading this
blog’s post to be updated daily. It contains nice data.
This post is priceless. How can I find out more?
Simply wish to say your article is as amazing. The clearness in your post is just great and i could assume
you are an expert on this subject. Well with your permission let me to
grab your feed to keep updated with forthcoming post.
Thanks a million and please carry on the gratifying work.
This is really interesting, You’re a very skilled blogger.
I’ve joined your rss feed and look forward to seeking more of your fantastic post.
Also, I’ve shared your site in my social networks!
Hey there! This is kind of off topic but I need some advice from an established blog.
Is it very difficult to set up your own blog? I’m not very techincal
but I can figure things out pretty fast. I’m thinking about creating my own but I’m not sure where to begin. Do you have any
ideas or suggestions? Thank you
Hello this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or
if you have to manually code with HTML. I’m starting a blog soon but
have no coding skills so I wanted to get advice from someone with experience.
Any help would be greatly appreciated!
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 there excellent website! Does running a blog like this require a great
deal of work? I’ve absolutely no knowledge of coding however
I was hoping to start my own blog in the near future.
Anyhow, should you have any ideas or tips for new blog owners please
share. I understand this is off subject but I simply needed to ask.
Cheers!
hello there and thank you for your information – I have definitely picked up
something new from right here. I did however expertise some technical issues
using this website, since I experienced to reload the site lots
of times previous to I could get it to load correctly.
I had been wondering if your web host is OK? Not that I am complaining, but slow loading
instances times will sometimes affect your placement in google and can damage your quality
score if advertising and marketing with Adwords. Anyway I am adding this RSS to my e-mail and could look out for
much more of your respective intriguing content.
Ensure that you update this again very soon.
Hey are using WordPress for your blog platform? I’m new to the
blog world but I’m trying to get started and set up my
own. Do you require any html coding knowledge to make your own blog?
Any help would be really appreciated!
Hello I am so delighted I found your blog page, I really found
you by mistake, while I was browsing on Google for something else,
Regardless I am here now and would just like to say thanks for a marvelous post
and a all round interesting blog (I also love the theme/design),
I don’t have time to read it all at the minute but I have bookmarked it and also
added in your RSS feeds, so when I have time I will be back to read much more, Please do
keep up the fantastic jo.
Hi there all, here every person is sharing such experience,
so it’s pleasant to read this website, and I used to pay a visit this weblog all the time.
Hi there every one, here every person is sharing these knowledge,
so it’s pleasant to read this webpage, and I used to pay a visit this webpage everyday.
Pretty nice post. I just stumbled upon your blog and wished to say that I have
really enjoyed browsing your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again soon!
Amazing! This blog looks just like my old one!
It’s on a totally different topic but it has pretty much the same layout and design. Excellent choice of colors!
What a stuff of un-ambiguity and preserveness of valuable know-how
about unexpected emotions.
Hi there, its fastidious article regarding media print, we all be familiar with
media is a impressive source of information.
Hello mates, its great post about tutoringand completely explained, keep it up
all the time.
Wonderful blog! I found it while surfing around on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Cheers
Awesome article.
Hi there, all is going fine here and ofcourse every one is sharing facts, that’s genuinely good, keep up
writing.