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

  1. Hey there, I think your site might be having browser compatibility
    issues. When I look at your blog in Ie, it looks fine
    but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, great blog!

  2. Dereketerm表示:

    can you get clomid tablets: clomid – how to buy clomid without prescription

  3. Markdob表示:

    Всем привет! Может кто знает, где найтиполезные блоги о недвижимости? Пока нашел – https://doors-pervouralsk.ru

  4. StevenNub表示:

    where can i get generic clomid now: clomid purchase online rex pharm – order clomid tablets

  5. Hi! Someone in my Facebook group shared this website with us
    so I came to give it a look. I’m definitely enjoying the information. I’m book-marking and
    will be tweeting this to my followers! Exceptional blog and terrific
    design.

  6. BrandenSlomy表示:

    amoxicillin 775 mg: Amoxicillin Com Pharm – amoxicillin 500mg price in canada

  7. Dereketerm表示:

    can you buy generic clomid without prescription: clomid online – where buy cheap clomid pill

  8. StevenNub表示:

    where to get amoxicillin over the counter: amoxil – amoxicillin 500mg no prescription

  9. Markdob表示:

    Всем привет! Может кто знает, где найтиразные статьи о недвижимости? Пока нашел – https://doors-pervouralsk.ru

  10. Markdob表示:

    Приветствую. Подскажите, где почитатьразные статьи о недвижимости? Пока нашел – https://doors-pervouralsk.ru

  11. Dereketerm表示:

    amoxicillin pharmacy price: Amoxicillin for sale – order amoxicillin online

  12. Markdob表示:

    Всем привет! Подскажите, где найтиполезные блоги о недвижимости? Пока нашел – https://doors-pervouralsk.ru

  13. casinoapp表示:

    I used to be able to find good information from your blog
    articles.

  14. Medicines information sheet. Generic Name.
    where can i get cheap nortriptyline online
    All about medicines. Read information now.

  15. Markdob表示:

    Всем привет! Может кто знает, где почитатьразные статьи о недвижимости? Сейчас читаю – https://doors-pervouralsk.ru

  16. Good day! I just wish to offer you a huge thumbs up for your excellent info you have got here on this post.
    I’ll be coming back to your web site for more soon.

  17. Blakesmema表示:

    кухни на заказ екатеринбург — Изготовление стильных и функциональных кухонь на заказ в Екатеринбурге.

  18. porn comic表示:

    Pretty element of content. I simply stumbled upon your blog and in accession capital to assert that I acquire in fact loved account your weblog
    posts. Anyway I will be subscribing to your feeds and even I achievement you get entry to constantly quickly.

  19. BrandenSlomy表示:

    get cheap clomid without rx: generic clomid – how to buy cheap clomid no prescription

  20. Hi! I understand this is sort of off-topic however I had to ask.

    Does running a well-established website like yours take
    a massive amount work? I’m brand new to writing a blog however I do write
    in my diary every day. I’d like to start
    a blog so I can easily share my experience and views online.
    Please let me know if you have any kind of ideas or tips for new aspiring
    bloggers. Appreciate it!

    my page; Pinoy SEO Services

  21. Dereketerm表示:

    can you buy cheap clomid now: rexpharm – where buy clomid without a prescription

發佈留言

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