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,420,600 Responses

  1. WilliamRhype表示:

    https://nolvadex.life/# who should take tamoxifen

  2. 3 Common Causes For Why Your Robot Vacuum Isn’t Working (And How To Fix It) Best Robot Vacuum For Pet Hair

  3. sash表示:

    You’ve Forgotten Window Glass Repairs: 10 Reasons Why
    You Don’t Really Need It sash

  4. RandallTaulp表示:

    ciprofloxacin order online: buy cipro online – ciprofloxacin mail online

  5. Costs表示:

    A Look Inside Replacing Volkswagen Key’s Secrets Of Replacing
    Volkswagen Key Costs

  6. bokep表示:

    Aw, this was a very nice post. Spending some time and actual effort to
    generate a really good article… but what can I say… I put things off a whole lot
    and never seem to get nearly anything done.

  7. Get Rid Of Avon Books Online: 10 Reasons That You No Longer Need It
    Avon Book March 2023

  8. Francesco表示:

    We are a group of volunteers and opening a new scheme
    in our community. Your site provided us with valuable info to work on. You’ve done a formidable job and our whole community will be thankful to you.

  9. 10 Things You Learned In Preschool That’ll Help You Understand
    Double Glazing Near Me Replace double Glazed Window

  10. Ronaldpsymn表示:

    http://lisinopril.network/# can i buy lisinopril in mexico

  11. See What Dreametech L30 Ultra Tricks The Celebs Are
    Utilizing Dreametech L30 (https://Upright-Elephant-H5Qqtz.Mystrikingly.Com)

  12. Davidtes表示:

    generic propecia online cost of cheap propecia cost of cheap propecia price

  13. WilliamRhype表示:

    http://finasteride.store/# order propecia without a prescription

  14. После подтверждения от казино
    вход в личный кабинет открыт.

  15. This Week’s Most Popular Stories Concerning Mobility Scooters
    Shops Near Me Mobility Scooters near me

  16. RandallTaulp表示:

    cost cheap propecia without insurance: cheap propecia for sale – generic propecia price

  17. Sex Machine In My Area Tools To Help You Manage Your Everyday Lifethe Only Sex Machine In My Area
    Trick That Everybody Should Know sex Machine in my Area

  18. Your Family Will Thank You For Having This Replacement Double Glazed Glass Only Near Me replacing double Glazed glass

  19. Davidtes表示:

    cost of cheap propecia tablets buying propecia online cost propecia without prescription

  20. Lavina表示:

    A Trip Back In Time What People Said About Vauxhall Corsa Replacement Key 20 Years Ago replacement car key vauxhall, Lavina,

  21. Auto Door Lock Repair 10 Things I’d Like To Have Known In The Past key lock repair near me

  22. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# cipro for sale

  23. Davidtes表示:

    lisinopril 20 mg online lisinopril 12.5 tablet lisinopril 10 mg price

  24. 9 . What Your Parents Taught You About Best Robot Vacuum And Mop Self Empty Best robot vacuum and mop self empty

  25. kilowatt Case表示:

    The Most Worst Nightmare About Counter Strike
    Online Come To Life kilowatt Case

  26. Five Killer Quora Answers To Key Cuts For Cars key Cuts for Cars

  27. Ronaldpsymn表示:

    http://lisinopril.network/# zestril cost

  28. Все транзакции зашифрованы, и ваша личная
    информация остается конфиденциальной.

  29. Window Repair表示:

    5 Laws That Can Help The Double Glazing Windows Repairs Industry Window Repair

發佈留言

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