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

  1. Wow, awesome blog layout! How lengthy have you ever been running a blog
    for? you make running a blog glance easy. The full look of your web site is magnificent, let
    alone the content material!

    Also visit my web site: new release dvd

  2. Josephmax表示:

    https://vavada-kazi.ru/# вавада казино

  3. Mason Abbott表示:

    I just finished a bathroom renovation in my Chicago home Revive 360 bathroom remodeling

  4. DarioTob表示:

    vavada: vavada-kazi.ru – казино вавада

  5. Iyannaobesk表示:

    mobile poker club скачать на андроид бесплатно
    mobile poker club скачать на андроид на русском

  6. Josephmax表示:

    https://vavada-kazi.ru/# вавада казино онлайн

  7. you can to take this PDE5 inhibitor at a higher high dose approximately for half an hour before sexual contact so that no more than once per day.https://riskybusiness.org/

  8. HenrydeT表示:

    pinup пин ап вход пин ап казино

  9. LeonardKnism表示:

    viktorova-ts.ru – Доверяйте профессионалам в создании кухни вашей мечты.

  10. DarioTob表示:

    pinup: pinup – пин ап казино онлайн

  11. curi movie表示:

    I really love your site.. Pleasant colors & theme.
    Did you make this web site yourself? Please reply back as I’m wanting to create my very
    own site and would like to know where you got this from
    or exactly what the theme is called. Thank you!

  12. Jamesdiure表示:

    пин ап зеркало: пин ап вход – pinup

  13. Petir138表示:

    I’ll right away snatch your rss feed as I can not in finding your email subscription hyperlink
    or newsletter service. Do you have any? Kindly let me know
    in order that I could subscribe. Thanks.

  14. CurtisIncix表示:

    пин ап зеркало: пин ап зеркало – пин ап казино официальный сайт

  15. Managing https://gitea.elatteria.com/tracicix196592 sugar levels is a critical aspect of maintaining overall health, especially for individuals with diabetes or those at risk of developing the condition.

  16. Nikefut表示:

    dark web access tor markets links darknet sites

  17. Hey! I know this is somewhat off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having trouble finding
    one? Thanks a lot!

    Feel free to surf to my web-site: specialized dementia care

  18. Hello everybody, here every person is sharing these kinds
    of familiarity, therefore it’s nice to read this blog, and I used to visit this weblog all
    the time.

  19. Iyannaobesk表示:

    jackpoker скачать
    jackpoker скачать онлайн

  20. Blakesmema表示:

    http://shth.ru/ — Подробности о дизайне и изготовлении кухонь.

  21. Write more, thats all I have to say. Literally, it seems
    as though you relied on the video to make your point.

    You obviously know what youre talking about, why throw away your intelligence on just posting
    videos to your blog when you could be giving us something enlightening to read?

    Also visit my website; dvd distributors

  22. slot online表示:

    This is really attention-grabbing, You’re a very skilled blogger.
    I have joined your rss feed and look forward to in the hunt for more of your fantastic post.
    Additionally, I’ve shared your website in my social networks

  23. tototok.com表示:

    Have you ever considered creating an ebook or guest authoring on other websites?
    I have a blog based on the same subjects you discuss and would love to have you share
    some stories/information. I know my subscribers would appreciate your work.
    If you’re even remotely interested, feel free to shoot me an email.

發佈留言

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