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,231,061 Responses

  1. Corey表示:

    What’s Taking place i’m new to this, I stumbled upon this
    I’ve found It absolutely useful and it has helped me
    out loads. I’m hoping to give a contribution & aid other customers
    like its helped me. Great job.

  2. Mesothelioma lawyers aid the families of victims throughout the legal process.
    They prepare cases, file lawsuits, and asbestos settlement trust claims they
    negotiate settlements and fight for fair verdicts in trial.

  3. Drstobeese表示:

    Macrobid: giant eagle pharmacy hours – boots pharmacy antibiotic eye drops chloramphenicol

  4. Since the admin of this site is working, no question very shortly it will be renowned, due to its feature contents.

  5. Great beat ! I wish to apprentice while you
    amend your site, how could i subscribe for a blog web site?
    The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear
    idea

  6. Highly descriptive blog, I liked that a lot. Will there be a part 2?

  7. Easydrorbix表示:

    trazodone price pharmacy: pharmacy online mexico – compare prescription prices

  8. Easydrorbix表示:

    24 hour pharmacy: antibacterial – tretinoin cream pharmacy

  9. The 10 Most Scariest Things About Situs Toto Login situs toto
    login, https://www.98e.fun/,

  10. 20 Tips To Help You Be More Effective At Lexus Key
    Replacement Uk cost to replace lexus key fob
    willysforsale.com

  11. Easydrorbix表示:

    buy priligy online pharmacy: AebgEmoli – accredo pharmacy

  12. sofas Sale表示:

    Five Killer Quora Answers On Sofas Sale sofas Sale

  13. Thomasalism表示:

    Opened up interesting material – I recommend sharing this discovery http://samara.listbb.ru/viewtopic.php?f=3&t=512

  14. Titration ADHD Meds Tools To Improve Your Daily Lifethe One Titration ADHD Meds Trick Every Person Should
    Know Titration adhd Meds

  15. Ten Adhd Adult Assessment That Will Actually Change Your Life adhd Assessment Uk adults

  16. See What Best Meds For Anxiety Disorders Tricks The Celebs Are Using best meds for anxiety disorders

  17. Drstobeese表示:

    good neighbor pharmacy loratadine: viagra american pharmacy – pompharmacy viagra

  18. Nice post. I learn something totally new and challenging
    on sites I stumbleupon every day. It will always be useful to
    read content from other authors and use a little something from their sites.

  19. dich vu seo表示:

    An intriguing discussion is worth comment. I think that you need
    to write more on this topic, it may not be a taboo
    subject but usually folks don’t discuss such subjects. To the next!
    Many thanks!!

  20. Eltonwag表示:

    мужской ретрит https://ретриты.рф

  21. Eltonwag表示:

    психологический ретрит https://ретриты.рф

  22. Angelita表示:

    It’s perfect time to make some plans for the future and it is time to be
    happy. I’ve read this post and if I could I desire to suggest you few interesting things or
    advice. Maybe you can write next articles referring to this article.
    I want to read even more things about it!

  23. Andreas表示:

    Right now it seems like Movable Type is the preferred blogging platform available right
    now. (from what I’ve read) Is that what you are using on your blog?

  24. Anal Jewelry表示:

    How Much Do Remote Control Anal Plug Experts Make? Anal Jewelry

發佈留言

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