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,247,345 Responses

  1. websell begok表示:

    When I originally commented I clicked the
    “Notify me when new comments are added” checkbox and now each
    time a comment is added I get three emails with the same
    comment. Is there any way you can remove me from that service?

    Bless you!

  2. dewascatter表示:

    I love your blog.. very nice colors & theme. Did you create this website yourself
    or did you hire someone to do it for you? Plz answer back as I’m
    looking to construct my own blog and would like to know where u
    got this from. thanks a lot

  3. AlbertDuM表示:

    cytotec pills buy online: cytotec buy online usa – Abortion pills online

  4. DarrenUnjuh表示:

    http://plavixclo.com/# plavix best price

  5. Wow! At last I got a web site from where I be able to actually get useful information concerning my study and knowledge.

  6. BrandenSlomy表示:

    can i buy cheap clomid without prescription: generic clomid – get cheap clomid

  7. DarrenUnjuh表示:

    https://iverfast.com/# buy ivermectin cream

  8. JacobLoody表示:

    https://www.taxipuma.ru/ — ваш надежный партнер в создании идеальной кухни

  9. DarrenUnjuh表示:

    https://iverfast.com/# stromectol without prescription

  10. BernardMop表示:

    buy cytotec cyt premium Abortion pills online

  11. Sazrmlb表示:

    Приобретение диплома ПТУ с сокращенной программой обучения в Москве
    armgame.forumex.ru/viewtopic.php?f=78&t=176

  12. AlbertDuM表示:

    cytotec abortion pill: Cytotec 200mcg price – buy cytotec pills

  13. padlet.com表示:

    Triangle Billiards & Bar Stools
    1471 Nisson Ꮢd, Tustin,
    CA 92780, United Ⴝtates
    +17147715380
    Pool Table Discounts [padlet.com]

  14. Stephenideox表示:

    Бесплатный информационный сервис, предоставляющий контент и справочную информацию о инвестировании.
    инвестиция заемных средств

  15. Thank you! A good amount of write ups.

  16. AlbertDuM表示:

    ivermectin oral: cheapest Ivermectin – minocycline 50mg online

  17. Hello it’s me, I am also visiting this website daily, this website is really pleasant and the visitors are in fact sharing nice thoughts.

  18. Hi, I do believe this is an excellent website.
    I stumbledupon it 😉 I am going to return yet again since
    I book marked it. Money and freedom is the best way to change,
    may you be rich and continue to guide others.

  19. AlbertDuM表示:

    cytotec pills buy online: cytotec online – Abortion pills online

發佈留言

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