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,123,331 Responses

  1. Unquestionably believe that which you said.
    Your favorite reason appeared to be on the internet the
    easiest thing to be aware of. I say to you, I certainly get annoyed while people think about worries that they plainly do not know about.

    You managed to hit the nail upon the top and defined out
    the whole thing without having side effect , people could
    take a signal. Will likely be back to get more. Thanks

    Also visit my website: fitspresso reddit weight loss

  2. Loan Consolidation Solutions For Individuals 대출 갈아타기

  3. Darrelherse表示:

    oyun siteleri slot: deneme veren slot siteleri – deneme bonusu veren slot siteleri

  4. imd スイス表示:

    That includes a large-mini-fridge-sized battery, an energy meter and a large-backpack-sized inverter to convert the battery’s direct current (DC) back into alternating current (AC) that the house can use.

  5. Question: How Much Do You Know About Audi Lost Key?
    Audi Replacement Key Cost (O39Akk533B75Wnga.Kr)

  6. Stand Strong Fencing
    Nashville, Tennessee 37201, Unites Ѕtates
    16154311511
    Strong privacy fence options

  7. Darrelherse表示:

    en iyi slot siteleri 2024: casino slot siteleri – guvenilir slot siteleri

  8. JamesscarP表示:

    לבילוי בכרמל נערות ליווי בחיפה תמיד נמצאות כאן בשביל לפנק את הגברים ולעשות להם טוב. בין אם זה יום חורף קר או שיא הקיץ, סתיו הכל על מנת שתרגיש טוב. לבלות עם נערות ליווי בקריות חולם על בילוי מפנק ברמה גבוהה? אתה עומד לקבל את מה שאתה רוצה ועוד הרבה resource

  9. Cannabidiol, commonly known as CBD, has surged in popularity
    due to its potential therapeutic benefits.
    Canna Plus CBD is one such product that seeks to offer these benefits through its unique formulation.

  10. My spouse and I absolutely love your blog and find a lot of your post’s to be just what I’m looking
    for. Would you offer guest writers to write content in your case?
    I wouldn’t mind publishing a post or elaborating on a lot of the subjects you write related to here.

    Again, awesome weblog!

  11. Darrelherse表示:

    en iyi slot siteleri: slot oyun siteleri – slot oyunlar? siteleri

  12. Darrelherse表示:

    yeni slot siteleri: yeni slot siteleri – en guvenilir slot siteleri

  13. Keto Plus表示:

    The ketogenic diet, commonly known as the keto diet, has gained significant popularity for its potential benefits in weight
    loss, improved mental clarity, and enhanced energy
    levels.

  14. This is a topic that’s near to my heart… Thank you! Exactly where are your contact details though?

  15. Darrelherse表示:

    guvenilir slot siteleri 2024: slot oyunlar? siteleri – en iyi slot siteler

  16. erec prime表示:

    Really no matter if someone doesn’t understand after that its up to
    other users that they will assist, so here it takes place.

    Feel free to visit my blog post erec prime

  17. Darrelherse表示:

    deneme bonusu veren slot siteleri: yasal slot siteleri – slot siteleri guvenilir

  18. Uazrhzu表示:

    Привет!
    Заказать диплом любого ВУЗа.
    telegra.ph/diplom-ohrannika-novogo-obrazca-kupit-08-13-9

  19. alexisneiers表示:

    If some one wants expert view regarding running a blog afterward i recommend him/her to
    pay a quick visit this webpage, Keep up the nice job.

  20. Slot Maxwin表示:

    Great weblog here! Also your web site a lot up very fast!
    What host are you the usage of? Can I get your affiliate
    link for your host? I want my web site loaded up as quickly as
    yours lol

  21. Greg表示:

    Keep in mind, a referral letter is not the single evidence to verify your job remains in nationwide passion.

    my web-site … EB2 Visa business plan (Greg)

發佈留言

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