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...

872,570 Responses

  1. Maricruz表示:

    What’s up to all, the contents existing at this web site
    are truly remarkable for people knowledge, well, keep up the nice work fellows.

  2. What’s up to every one, since I am really eager of reading this blog’s post
    to be updated regularly. It carries fastidious information.

  3. www.ted.com表示:

    Wonderful post! We are linking to this particularly great post on our website.
    Keep up the good writing.

  4. Hi outstanding blog! Does running a blog similar to
    this require a great deal of work? I’ve virtually no knowledge
    of coding but I was hoping to start my own blog in the near future.
    Anyways, should you have any recommendations or techniques
    for new blog owners please share. I understand this is off subject nevertheless I simply wanted to ask.
    Thanks!

  5. discuss表示:

    Very good information. Lucky me I ran across your blog by accident (stumbleupon).

    I’ve bookmarked it for later!

  6. Hello my family member! I wish to say that this article is awesome, great written and come with almost all important
    infos. I’d like to look extra posts like this .

  7. www.fcc.gov表示:

    Hey I am so grateful I found your website, I really found you by accident, while I
    was researching on Yahoo for something else, Anyhow I am
    here now and would just like to say thanks for a tremendous post
    and a all round entertaining blog (I also love the theme/design), I don’t
    have time to go through it all at the moment but I have bookmarked
    it and also added your RSS feeds, so when I have time I will be back to
    read a lot more, Please do keep up the excellent
    work.

  8. Good replies in return of this matter with real arguments and
    describing all concerning that.

  9. www.4mark.net表示:

    Great post. I am dealing with some of these issues as well..

  10. At this time it appears like Movable Type is the best blogging platform out there right now.
    (from what I’ve read) Is that what you are using on your blog?

  11. kikipedia.win表示:

    Hi there, I read your blog like every week. Your writing style is witty, keep up the good work!

  12. Hi, all is going sound here and ofcourse every one is
    sharing facts, that’s in fact good, keep up writing.

  13. The other day, while I was at work, my cousin stole my apple ipad
    and tested to see if it can survive a 25 foot drop, just so
    she can be a youtube sensation. My apple ipad is now destroyed and she
    has 83 views. I know this is totally off topic but I had to share it with someone!

  14. Magnificent beat ! I wish to apprentice while you amend your website, how could i
    subscribe for a blog site? The account aided me a acceptable deal.
    I had been a little bit acquainted of this your broadcast offered bright clear concept

  15. Thanks in support of sharing such a good thinking, piece of writing is good, thats
    why i have read it completely

  16. I am regular visitor, how are you everybody? This post posted at this site is genuinely pleasant.

  17. For hottest information you have to pay a visit internet and on internet I found this web site as a finest web site for most recent
    updates.

  18. Superb, what a blog it is! This website provides
    helpful information to us, keep it up.

  19. What i don’t understood is in fact how you are no longer actually a lot more neatly-preferred than you might be now.
    You are very intelligent. You realize therefore considerably with regards
    to this topic, made me in my view believe it from so many
    varied angles. Its like women and men are not interested unless it’s something to do with Girl gaga!

    Your own stuffs nice. Always handle it up!

  20. Hello there! Do you know if they make any plugins to assist
    with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Many thanks!

  21. This is a topic which is close to my heart… Cheers!
    Exactly where are your contact details though?

  22. This is really attention-grabbing, You’re an excessively skilled
    blogger. I’ve joined your rss feed and sit up for seeking extra of your magnificent post.

    Additionally, I’ve shared your web site in my social networks

  23. What’s up to all, the contents present at this site are genuinely amazing for people
    experience, well, keep up the good work fellows.

  24. discuss表示:

    Hi there, of course this paragraph is really fastidious and I have
    learned lot of things from it on the topic of blogging.

    thanks.

  25. Thanks for finally talking about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!

  26. discuss表示:

    Hello, I want to subscribe for this web site to take most up-to-date updates,
    therefore where can i do it please help out.

  27. You can certainly see your expertise in the
    article you write. The arena hopes for even more passionate writers such as you who
    aren’t afraid to say how they believe. At all times go after your
    heart.

  28. I pay a visit every day some web sites and blogs
    to read posts, but this website provides quality based
    content.

  29. My brother recommended I would possibly like this web site.
    He was entirely right. This submit truly made my day. You can not consider just how much time I had spent for this information! Thank you!

  30. Wonderful beat ! I wish to apprentice even as you amend your site, how could i subscribe
    for a weblog website? The account aided me a applicable
    deal. I were a little bit acquainted of this your
    broadcast offered bright clear idea

發佈留言

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