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

You may also like...

1,233,633 Responses

  1. I’m gone to convey my little brother, that he should also pay a quick visit this blog on regular basis to take updated from
    most up-to-date news update.

  2. Howdy! This post couldn’t be written any better! Reading this post reminds me of my old room mate!

    He always kept chatting about this. I will forward this post to him.
    Fairly certain he will have a good read. Thanks for sharing!

  3. If you want to get much from this paragraph then you have to apply these methods to your
    won blog.

  4. Hi, just wanted to say, I liked this article. It was helpful.
    Keep on posting!

  5. cutt.ly表示:

    Wow, this paragraph is pleasant, my younger sister is
    analyzing these kinds of things, thus I am going to tell her.

  6. Hi to all, how is everything, I think every one is getting more from this web page, and your views are nice in support of
    new people.

  7. Great blog right here! Also your website a lot up very fast!
    What web host are you using? Can I am getting your affiliate link
    to your host? I desire my website loaded up as quickly as yours lol

  8. My partner and I stumbled over here by a different website and thought I may as well check things out.
    I like what I see so i am just following you. Look forward to finding out about your web page again.

  9. Yesterday, while I was at work, my cousin stole my iphone and tested
    to see if it can survive a forty foot drop, just so
    she can be a youtube sensation. My apple ipad is now broken and
    she has 83 views. I know this is completely off topic but I had to
    share it with someone!

  10. I’m really impressed with your writing abilities as well as with the format in your weblog.
    Is this a paid topic or did you modify it yourself?
    Either way stay up the excellent high quality writing, it’s
    rare to look a great weblog like this one today..

  11. Amazing blog! Is your theme custom made or did you download
    it from somewhere? A design like yours with a few simple adjustements would really make my blog
    shine. Please let me know where you got your theme.
    With thanks

  12. For latest information you have to pay a quick visit web and
    on world-wide-web I found this website as a most excellent web page for most up-to-date updates.

  13. anotepad.com表示:

    First of all I would like to say terrific blog!

    I had a quick question which I’d like to ask if you do not mind.

    I was interested to know how you center yourself and clear
    your head before writing. I have had a difficult time clearing my mind in getting my ideas out.
    I do take pleasure in writing however it just seems like the first
    10 to 15 minutes are generally lost just trying to figure out how to begin. Any ideas or tips?
    Thanks!

  14. Hi there to every one, the contents present at this site are really amazing for people experience,
    well, keep up the nice work fellows.

  15. I’m not sure where you are getting your information, but great topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for magnificent information I was looking for this information for
    my mission.

  16. botdb.win表示:

    I’m very pleased to uncover this page. I want to to thank
    you for your time for this particularly wonderful read!! I definitely
    loved every part of it and I have you saved as a favorite to
    look at new information on your blog.

  17. Howdy! I could have sworn I’ve been to this web site before but after going through
    some of the articles I realized it’s new to me.
    Anyways, I’m certainly delighted I came across it and I’ll be book-marking it and checking back frequently!

  18. Hi, i think that i saw you visited my weblog so i came to “return the favor”.I’m attempting to find
    things to improve my website!I suppose its ok
    to use a few of your ideas!!

  19. This information is worth everyone’s attention. When can I find out more?

  20. Everything is very open with a very clear explanation of the issues.
    It was really informative. Your website is extremely helpful.

    Thanks for sharing!

  21. Wow! This blog looks exactly like my old one!
    It’s on a totally different topic but it has pretty much the
    same layout and design. Wonderful choice of colors!

  22. I’ve learn a few excellent stuff here. Definitely price bookmarking for revisiting.
    I wonder how so much attempt you place to create such a wonderful
    informative web site.

  23. What’s up i am kavin, its my first occasion to commenting anywhere, when i
    read this paragraph i thought i could also create comment due to this sensible piece of writing.

  24. discuss表示:

    Since the admin of this site is working, no question very soon it will be well-known, due to its feature contents.

  25. Hi there everyone, it’s my first pay a quick visit at this web site,
    and article is genuinely fruitful in favor of me, keep up posting such articles.

  26. Elizabet表示:

    I’m truly enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more pleasant for me to come here
    and visit more often. Did you hire out a developer to
    create your theme? Fantastic work!

  27. www.fcc.gov表示:

    Greetings! Very useful advice within this article! It is the little changes that make the biggest
    changes. Many thanks for sharing!

  28. Great blog! Is your theme custom made or did you
    download it from somewhere? A design like yours with a few simple adjustements would really make
    my blog jump out. Please let me know where you got
    your theme. Appreciate it

  29. Informative article, exactly what I needed.

  30. Hello there, just became aware of your blog through Google, and found that it is truly informative.
    I am going to watch out for brussels. I’ll appreciate if
    you continue this in future. Numerous people will be benefited from your writing.
    Cheers!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。