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,371,358 Responses

  1. Excellent website. Lots of helpful info here.
    I’m sending it to some friends ans also sharing in delicious.

    And naturally, thanks for your effort!

  2. Dereketerm表示:

    prednisone cost canada: generic Prednisone – prednisone 20 mg tablets

  3. электронный карниз для штор электронный карниз для штор .

  4. Your way of explaining all in this piece of writing is truly good,
    every one be able to easily know it, Thanks a lot https://fenestrationdessommets.com/nos-realisations/

  5. For hottest news you have to visit internet and on web I found
    this web page as a finest site for hottest updates.

  6. BrandenSlomy表示:

    can you buy amoxicillin over the counter in canada: amoxil com pharm – can i buy amoxicillin over the counter in australia

  7. Pretty nice post. I just stumbled upon your blog and wished to say that
    I’ve really enjoyed browsing your blog posts. In any case
    I’ll be subscribing to your feed and I hope you write again soon!

  8. Heya i am for the first time here. I came
    across this board and I find It really useful & it helped me out a lot.
    I hope to give something back and help others like you aided me.

  9. Jerri表示:

    20 Myths About Adhd Assessment Adult: Busted should i get assessed for adhd
    [Jerri]

  10. BrandenSlomy表示:

    clomid generic: clomid rex pharm – can i buy generic clomid no prescription

  11. Curtisduh表示:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  12. Aidan206表示:

    You completed certain reliable points there. I did a search on the subject and found nearly all persons will agree with your blog.

  13. Williams表示:

    Feel free to surf to my blog post … christian online
    dating sites (Williams)

  14. StevenNub表示:

    max pharm: max pharm – max pharm

  15. BrandenSlomy表示:

    prednisone tablets india: cheap prednisone – buy prednisone with paypal canada

  16. ADHD Assessment Private Tools To Improve Your Daily Life ADHD Assessment Private Trick That Every Person Must Know Adhd Assessment private

  17. Nikefut表示:

    dark web market darknet markets https://darknetmarketstore.com/ – deep web search

  18. Markdob表示:

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

  19. Markdob表示:

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

  20. Individualize your pitch to resolve each potential client’s certain requirements.

    Feel free to surf to my webpage … objectstorage.ap-melbourne-1.oraclecloud.com

  21. BrandenSlomy表示:

    amoxicillin 500mg capsule cost: cheap amoxil – generic amoxicillin over the counter

  22. Drugs information leaflet. Cautions.
    how to get cheap allegra without insurance
    Best information about medication. Read information here.

發佈留言

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