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,236,349 Responses

  1. I have been browsing online more than three hours today, yet I never found any interesting article like yours.

    It is pretty worth enough for me. Personally, if all webmasters and bloggers made good content as you did, the net
    will be much more useful than ever before.

  2. Richardbat表示:

    вавада зеркало на сегодняшний день тг вавады

  3. Wilburn表示:

    20 Car Diagnostics Near Me Uk Websites Taking The Internet By Storm diagnostic check (Wilburn)

  4. bokep indo表示:

    It’s an remarkable article in favor of all the online
    visitors; they will take benefit from it I am sure.

  5. Hello there! I just would like to give you a huge thumbs up
    for your great info you have here on this post. I’ll be returning to your web
    site for more soon.

  6. KevinSounc表示:

    http://denemebonusuverensiteler.win/# bonus veren siteler

  7. gay_nvpr表示:

    Ставки на спорт для ЛГБТ сообщества | Спортивные ставки для LGBTQ+ на 1xbet | Gay-friendly 1xbet: ставки с комфортом | Секреты успешных ставок на спорт для ЛГБТ на 1xbet | 1xbet: надёжные ставки для геев
    porn gay in 1xbet https://1xbetcasinogayclub-ar.com/ .

  8. 10 Simple Steps To Start Your Own Replacement Mini Key Fob Business mini key replacement near Me

  9. This is a topic that is near to my heart… Thank you!
    Where are your contact details though?

  10. Donnell表示:

    Saved as a favorite, I love your web site!

  11. LarryRab表示:

    Женские вечерние платья https://vechernie-platya-princess.ru купить в интернет каталоге с доставкой в Москве. Брендовые женские вечерние платья в наличии и на заказ.

  12. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza mostbet

  13. It’s in point of fact a great and helpful piece
    of information. I am glad that you shared this helpful info with us.
    Please stay us up to date like this. Thanks for sharing.

  14. Darrelherse表示:

    guvenilir slot siteleri 2024: deneme veren slot siteleri – slot siteleri bonus veren

  15. The 12 Worst Types Key For Mercedes Accounts You Follow On Twitter mercedes replacement key

  16. Right away I am ready to do my breakfast, later than having my breakfast coming over again to read additional news.

  17. slot表示:

    untrustable

  18. xxx表示:

    untrustable

  19. Sazrdce表示:

    Как избежать рисков при покупке диплома колледжа или ПТУ в России

    myteana.ru/forums/index.php?autocom=gallery&req=si&img=5311

  20. Hello, everything is going well here and ofcourse every one is sharing information, that’s truly good, keep up writing.

  21. Guide To Lexus Key Replacement Uk: The Intermediate Guide For Lexus Key
    Replacement Uk Lexus key Replacement uk

  22. 15 Things To Give The Mesothelioma Law Firm Lover In Your Life Mesothelioma lawsuit

  23. Darrelherse表示:

    yeni slot siteleri: en guvenilir slot siteleri – slot siteleri bonus veren

  24. Guide To Lexus Key Fob Price: The Intermediate Guide In Lexus Key
    Fob Price Lexus key fob price

  25. Gerarddoorp表示:

    Excessive passion can distance to heat-related illnesses such as quicken forth and heatstroke. W populations embody the old fogies, unfledged children, and individuals with chronic illnesses https://excessiveheatawareness.ru/.

  26. xxx表示:

    untrustable

發佈留言

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