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,227,698 Responses

  1. This blog was… how do you say it? Relevant!!
    Finally I’ve found something that helped me.
    Thanks!

    Also visit my site … cannabis Australia

  2. Сюда входят ежедневные фрироллы, еженедельные и ежемесячные турниры и даже специальные мероприятия.

  3. The Best Software Backlink It’s What Gurus Do 3 Things Best automated Backlink Software

  4. RandallTaulp表示:

    generic propecia tablets: cost of generic propecia for sale – buy generic propecia prices

  5. You’ll Be Unable To Guess Double Glazing Repair Leeds’s
    Secrets Double Glazing Repair Leeds

  6. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen depression

  7. I’m really impressed with your writing skills and also with the layout on your
    weblog. Is this a paid theme or did you customize it yourself?

    Anyway keep up the excellent quality writing, it’s rare
    to see a nice blog like this one these days.

  8. play fortuna表示:

    Добавляются новинки в коллекцию развлечений Плей Фортуна с уникальным геймплеем и продуманным набором технических параметров.

  9. Why How To Get Diagnosed With ADHD In Adults Could Be More Dangerous
    Than You Thought adhd private diagnosis near me

  10. Greetings I am so delighted I found your blog, I really found you
    by error, while I was researching on Google for something else, Anyways I am here now and would just like to
    say many thanks for a remarkable post and a all round interesting blog (I also love the theme/design),
    I don’t have time to look over it all at the moment but I have book-marked 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 excellent b.

  11. wikimapia.org表示:

    What Is The Reason? Diagnostic Check Near Me Is Fast Becoming The Hot Trend For 2022 diagnostic scan (wikimapia.org)

  12. How To Build A Successful Railroad Injuries Settlement If You’re
    Not Business-Savvy Railroad Injuries Lawyer

  13. Audi q5 key表示:

    What Is Audi Convenience Key? And How To Make Use Of It Audi q5 key

  14. 9 Lessons Your Parents Taught You About Mesothelioma Compensation Mesothelioma – highclassps.com,

  15. Window Repair表示:

    You’ll Never Be Able To Figure Out This Window Repair Near Me’s Tricks
    Window Repair

  16. Syair SDY表示:

    I simply could not go away your web site prior to suggesting
    that I really enjoyed the standard information a person supply
    for your guests? Is gonna be back regularly in order to inspect new
    posts

  17. 25 Surprising Facts About Fiat 500 Key Replacement fiat key Fob Programming

  18. Don’t Make This Mistake With Your Car Key Repair Service car Key repair Company

  19. RandallTaulp表示:

    medication lisinopril 20 mg: lisinopril brand name uk – zestril

  20. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen moa

  21. RandallTaulp表示:

    nolvadex generic: nolvadex d – tamoxifen side effects forum

  22. See What Replacement Key Bmw Tricks The Celebs Are Using
    replacement key bmw

  23. What Are The Reasons You Should Be Focusing On Making Improvements In Key Replacement Bmw bmw key replacement

  24. WilliamRhype表示:

    https://ciprofloxacin.tech/# where can i buy cipro online

  25. What You Can Use A Weekly Mesothelioma Law Firm Project
    Can Change Your Life Asbestos attorney

  26. Davidtes表示:

    buy generic propecia buying generic propecia without dr prescription cost generic propecia without prescription

  27. Mozelle表示:

    Best Coffee Machines Explained In Less Than 140 Characters automatic coffee makers – Mozelle,

  28. Hi there, always i used to check weblog posts here early in the dawn, since i like
    to gain knowledge of more and more.

    Feel free to surf to my website … HCG Injections Shop Online

發佈留言

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