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,911 Responses

  1. Scottdaurf表示:

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

  2. We stumbled over here by a different page and thought I might
    check things out. I like what I see so now i am following you.
    Look forward to looking at your web page repeatedly.

  3. Lesliefem表示:

    1хбет зеркало 1xbet официальный сайт 1xbet зеркало рабочее на сегодня

  4. Awesome blog! Do you have any helpful hints for aspiring
    writers? I’m planning to start my own blog soon but I’m a little lost on everything.
    Would you advise starting with a free platform like
    Wordpress or go for a paid option? There are so many choices out there that I’m completely overwhelmed ..
    Any ideas? Thank you!

  5. Scottdaurf表示:

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

  6. kontol表示:

    Appreciate the recommendation. Let me try it out.

  7. In the fast-evolving world of nutritional supplements, Sweet Restore Glycogen Support
    Supplement has garnered attention for its promise of bolstering energy reserves and aiding recovery,
    especially for athletes and fitness enthusiasts.

  8. Scottdaurf表示:

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

  9. IDEALSPORT88表示:

    amazing very good

  10. Warrenmof表示:

    http://vavada.auction/# vavada зеркало

  11. Scottdaurf表示:

    pin up казино: пин ап казино – пин ап казино вход

  12. Greetings! Quick question that’s completely off topic. Do you know how to make your
    site mobile friendly? My blog looks weird when viewing from my iphone4.
    I’m trying to find a template or plugin that might be able to fix this problem.

    If you have any recommendations, please share.
    With thanks!

  13. Vidude表示:

    Hi, yeah this post is actually nice and I have learned lot of things from
    it concerning blogging. thanks.

  14. Warrenmof表示:

    https://vavada.auction/# вавада казино

  15. Scottdaurf表示:

    pin up казино: пин ап зеркало – пин ап

  16. Scottdiz表示:

    1xbet скачать: 1xbet зеркало рабочее на сегодня – 1xbet официальный сайт мобильная версия

  17. Scottdiz表示:

    1вин сайт: 1вин сайт – 1win вход

  18. Please let me know if you’re looking for a writer for your site.
    You have some really good posts and I feel I would be a good asset.
    If you ever want to take some of the load off, I’d love to write
    some articles for your blog in exchange for a link back to mine.
    Please send me an e-mail if interested. Many thanks!

  19. Thank you for delivering top-notch web creation together with
    WordPress care.

  20. Scottdaurf表示:

    pin up: пинап казино – пинап казино

  21. Pinterest表示:

    There is noticeably a bundle to identify about this.

    I assume you made some good points in features also.

    Feel free to surf to my page – Pinterest

  22. Hello there! This article could not be written any better!
    Going through this post reminds me of my previous roommate!
    He constantly kept preaching about this.
    I most certainly will forward this article to
    him. Fairly certain he’ll have a very good read.
    Thanks for sharing!

  23. Scottdaurf表示:

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

  24. 신규 하이카지노 플러스카지노 후속 카지노사이트 추천드리고 있습니다. 하이카지노

  25. Scottdaurf表示:

    пин ап казино вход: пинап казино – пин ап вход

  26. Greate article. Κeep posting ѕuch kind of іnformation on your site.
    Im reɑlly impressed by yοur site.
    Hello tһere, You’ve done а great job. Ӏ’ll ⅾefinitely digg
    iit ɑnd personally suggеst tօ my friends.
    Ӏ am sᥙгe they will be benefited from this
    site.

    Alsso visit mʏ blog post: akun slot gacor

  27. herpesyl表示:

    Hi, i read your blog from time to time and i own a similar one and i was just wondering if
    you get a lot of spam comments? If so how do you protect
    against it, any plugin or anything you can suggest?
    I get so much lately it’s driving me insane so any
    help is very much appreciated.

    Here is my page; herpesyl

  28. What i do not realize is in fact how you’re no longer really
    much more neatly-favored than you might be now. You are very
    intelligent. You already know therefore significantly
    in the case of this subject, produced me in my opinion consider it from a lot of numerous angles.

    Its like women and men aren’t fascinated unless it is one thing to accomplish with Girl gaga!
    Your individual stuffs great. All the time take care of it up!

發佈留言

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