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,523,478 Responses

  1. Davidmoits表示:

    pharmacies in mexico that ship to usa: mexico drug stores pharmacies – xxl mexican pharm

  2. Dannyfut表示:

    darknet market https://github.com/darknetmarketslinks/darknetmarketlinks – dark web search engines tor dark web

  3. Allow me show you in my testimonial of NewULife.

    Review my web site – cleaning services calgary

  4. Charleskepsy表示:

    canadian pharmacy coupon code https://familypharmacy.company/# Online pharmacy USA

  5. 30 Inspirational Quotes About Pallet Wood For Sale Pallet buying​

  6. Charleskepsy表示:

    canadian pharmacy without prescription http://easycanadianpharm.com/# easy canadian pharm

  7. 부산表示:

    Low Budget Travel Tips 부산

  8. I like it when individuals come together and share
    opinions. Great website, stick with it!

  9. Rodneytus表示:

    dark market list https://github.com/darkwebmarketslinks/darkwebmarkets – deep dark web dark web market

  10. WillieRap表示:

    easy canadian pharm: canada pharmacy reviews – canadian pharmacies compare

  11. MarkWat表示:

    Всем привет! Подскажите, где найтиразные статьи о займах? Сейчас читаю – allforb.ru

  12. Rodneytus表示:

    darknet drug market https://github.com/darkwebmarketslinks/darkwebmarkets – tor darknet darknet seiten

  13. NewULife Ranks. Welcome to my NewUlife Review.

    Here is my web blog; is epdm roofing any good

  14. slot777表示:

    Apkslot adalah situs games apk slot online paling banyak di
    download oleh masyarakat karena memiliki pelayanan profesional serta bonus besar untuk
    seluruh member, tersedia banyak pilihan permainan dengan grafik bagus
    memuaskan

  15. I have been surfing online more than three hours as
    of late, but I never found any fascinating article like
    yours. It is lovely price enough for me. In my opinion, if all web owners and bloggers made
    good content as you did, the net can be much more helpful
    than ever before.

  16. Emergency Treatment Training Brisbane is essential for everyone! I highly recommend having a look at the training courses provided at First Aid Courses Brisbane CBD

  17. NewULife Ranks. Welcome to my NewUlife Review.

    Also visit my webpage … vector animation software

  18. Charleskepsy表示:

    best online pharmacy no prescription https://megaindiapharm.shop/# Mega India Pharm

  19. WillieRap表示:

    mexico drug stores pharmacies: purple pharmacy mexico price list – xxl mexican pharm

  20. exist also concerns about the efficacy and safety of some https://martabak88slot.net/how-much-does-stem-cell-therapy-cost-understanding-3-2/, especially those that but they have
    not {{been|turned into} thoroughly tested in clinical trials.

  21. Porterfax表示:

    easy canadian pharm easy canadian pharm easy canadian pharm

  22. WillieRap表示:

    xxl mexican pharm: medication from mexico pharmacy – mexican mail order pharmacies

  23. 10 Things You’ve Learned In Preschool That Can Help You In Psychiatric Assessment psychiatric assessment glasgow

  24. It is the best time to make some plans for the longer
    term and it is time to be happy. I’ve learn this publish and if I could I want to counsel you some attention-grabbing issues or suggestions.
    Perhaps you can write subsequent articles referring to this article.

    I desire to learn even more things about it!

  25. Rodneytus表示:

    dark markets https://github.com/darkwebmarketslinks/darkwebmarkets – darknet search engine darknet seiten

  26. NewULife Ranks. Invite to my NewUlife Review.

    my blog post – cleaning services calgary

  27. online casino表示:

    You really make it seem so easy with your presentation but I find this
    matter to be really something which I think I would never understand.
    It seems too complicated and very broad for me. I’m looking forward for your next
    post, I’ll try to get the hang of it!

發佈留言

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