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,447,652 Responses

  1. WWW.alNA.SK表示:

    Somebody necessarily help to make severely articles I’d state.
    This is the very first time I frequented your website page and so far?
    I amazed with the research you made to create this particular put up incredible.
    Magnificent job! https://Www.alna.sk/index.php/component/k2/item/53?start=0

  2. Hi there! This article couldn’t be written much better!
    Looking through this post reminds me of my previous roommate!
    He constantly kept talking about this. I most certainly will
    forward this information to him. Pretty sure he’s going to have a great read.

    Thanks for sharing!

    my homepage 서귀포출장마사지

  3. kypit 1s_fgor表示:

    программа 1с купить программа 1с купить .

  4. kypit 1s_ycor表示:

    программа 1с купить программа 1с купить .

  5. kypit 1s_yqEn表示:

    1с бухгалтерия купить 1с бухгалтерия купить .

  6. pg99表示:

    Very good post. I definitely love this site.

    Keep it up!

  7. I seriously love your site.. Very nice colors & theme.
    Did you develop this site yourself? Please reply back as I’m trying to
    create my own personal website and want to find
    out where you got this from or exactly what the theme is named.
    Many thanks!

  8. Известковые растворы используются почти всегда при внутренних работах, главными преимуществами данного материала являются комфорт и.

    Take a look at my blog post: https://fishkaremonta.by/catalog/shtukaturka_zharostoykaya/

  9. CurtisBam表示:

    Indian Cert Pharm: Best online Indian pharmacy – indian pharmacy

  10. AndreDib表示:

    best canadian online pharmacy https://mexicaneasypharm.com/# Mexican Easy Pharm
    п»їlegitimate online pharmacies india

  11. AndreDib表示:

    canadian online pharmacy https://canadianmdpharm.com/# canadian pharmacy ltd
    indian pharmacy paypal

  12. kypit 1s_rxEn表示:

    настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .

  13. AndreDib表示:

    canada drug pharmacy https://mexicaneasypharm.shop/# Mexican Easy Pharm
    buy medicines online in india

  14. Hi there, this weekend is fastidious in support of me, since
    this occasion i am reading this impressive informative article here at my
    house.

  15. Hello, I read your new stuff regularly. Your story-telling style is witty, keep it up!

  16. kypit 1s_xior表示:

    1с бухгалтерия купить 1с бухгалтерия купить .

  17. CurtisBam表示:

    reputable mexican pharmacies online: Mexican Easy Pharm – Mexican Easy Pharm

  18. kypit 1s_ayor表示:

    1с бухгалтерия купить 1с бухгалтерия купить .

  19. kypit 1s_tuor表示:

    настроить 1с бухгалтерия цена https://svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513 .

  20. CurtisBam表示:

    Best Indian pharmacy: Indian Cert Pharm – IndianCertPharm

  21. CurtisBam表示:

    medicine in mexico pharmacies: best online pharmacies in mexico – Mexican Easy Pharm

  22. RTYHGHJKI8表示:

    I every time spent my half an hour to read this blog’s
    articles everyday along with a mug of coffee.

  23. kypit 1s_gior表示:

    1с предприятие купить программу http://www.svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513/ .

  24. 오피表示:

    Outdoor Bar Furniture Showing Entertaining Easy 오피

  25. CurtisBam表示:

    legitimate canadian pharmacy online: Canadian Md Pharm – canadianpharmacy com

發佈留言

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