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,233,769 Responses

  1. Hi there everyone, it’s my first go to see at this web site,
    and post is actually fruitful in support
    of me, keep up posting these types of articles
    or reviews.

    Feel free to surf to my webpage: เกม คาสิโน ได้เงินจริง

  2. What’s up to all, it’s actually a fastidious for me to pay a quick visit this web site, it includes precious Information.

  3. WnrPaish表示:

    Bonus pills added if you ivermectin oral at specially reduced prices

  4. ClydePab表示:

    deneme bonusu veren yeni siteler: deneme bonusu veren siteler 2024 – deneme bonusu veren siteler denemebonusu2026.com

  5. ClydePab表示:

    casino siteleri win: Casino Siteleri – casino siteleri win

  6. HassanbuifS表示:

    http://denemebonusuverensiteler.top/# deneme bonusu veren siteler yerliarama.org
    deneme bonusu veren siteler 2024

  7. ClydePab表示:

    matadorbet: matadorbet – matadorbet.bid

  8. Guide To Two Seater Fabric Sofa: The Intermediate Guide To Two Seater Fabric Sofa two seater fabric Sofa – https://aiwins.wiki/wiki/10_Top_Mobile_Apps_For_Small_Fabric_2_Seater_Sofa,

  9. CarrollUnton表示:

    deneme bonusu veren siteler mycbet.com http://matadorbet.bid/# matadorbet bid

  10. Trefscv表示:

    Диплом пту купить официально с упрощенным обучением в Москве
    gladpwnz.ru/forum/member.php?u=221749

  11. Upgrade Item表示:

    The 10 Most Scariest Things About Upgrade Item Upgrade Item

  12. CarrollUnton表示:

    deneme bonusu veren siteler yeni https://slot-tr.online/# en kazancl? slot oyunlar?

  13. An intriguing discussion is worth comment. I think that you
    should write more on this subject, it might not be a taboo matter
    but typically people do not talk about these issues.
    To the next! Kind regards!!

  14. Car Key Reprogramming: Myths And Facts Behind Car Key Reprogramming reprogramming keys
    for cars (https://king-wifi.win)

  15. This website definitely has all of the info I needed concerning
    this subject and didn’t know who to ask.

  16. ai-porn.uno表示:

    Thanks designed for sharing such a pleasant opinion, piece of writing is pleasant,
    thats why i have read it fully

  17. ckp.bedzin.pl表示:

    Asking questions are truly pleasant thing if you are not understanding anything entirely, except this piece of writing provides nice understanding even.

  18. Why Adding Smart Key Locksmith Near Me To Your Life Will Make All The Difference smart key for cars

  19. scam表示:

    Whats up very nice blog!! Guy .. Beautiful .. Amazing ..

    I’ll bookmark your blog and take the feeds also? I’m happy to seek
    out numerous useful info here in the publish, we want develop more techniques in this regard,
    thanks for sharing. . . . . .

  20. Excellent post. I was checking continuously this weblog and I
    am impressed! Extremely useful info particularly the final part
    🙂 I care for such information a lot. I was
    seeking this particular info for a long time. Thank you and best of luck.

  21. Thanks for another informative site. Where else may I get that type of information written in such a
    perfect means? I’ve a project that I am simply now operating on, and I’ve been on the glance out for such information.

  22. Asking questions are actually pleasant thing if you are not understanding anything totally, but this paragraph offers nice
    understanding yet.

  23. Trefnyl表示:

    Всё, что нужно знать о покупке аттестата о среднем образовании без рисков

    kbrg.ru/club/user/282/blog/?b24statAction=addLogEntry

  24. Shenasname.Ir表示:

    Why Link Togel Is Greater Dangerous Than You Think Link Togel Resmi (Shenasname.Ir)

  25. LarryCoell表示:

    ultrabet yeni giris 1125 ultrabet ultrabet yeni giris 1125

發佈留言

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