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...

899,034 Responses

  1. I like the helpful information you provide in your articles.

    I’ll bookmark your blog and check again here frequently.
    I’m quite sure I’ll learn a lot of new stuff
    right here! Best of luck for the next!

  2. %%

    Also visit my blog post … vegan gummies; http://soosunglift.gabia.io/,

  3. sexdoll sale表示:

    %%

    Also visit my website – sexdoll sale

  4. soma dosage表示:

    Soma (carisoprodol) can be taken as needed. It starts
    to work pretty quickly, and you don’t need to take it consistently for it
    to be effective.

    Capsule Icon
    Soma (carisoprodol) should only be used for
    a short period of time. It’s not meant to be used long-term because the types
    of pain it treats should not be chronic. If you have
    chronic muscle spasms, there might be a better alternative for you.

  5. I blog often and I seriously thank you for your content.
    Your article has truly peaked my interest. I will take a note of your website and keep checking for new information about once per week.
    I subscribed to your RSS feed too.

  6. Hectorrer表示:

    мега шишки mega market

  7. Craigbup表示:

    dark web sites name list dark web links

  8. Michaeljit表示:

    my canadian pharmacy canadian pharmacy for viagra

  9. I know thіs if offf topic but I’m looking into starting my oԝn weblog
    аnd ᴡas curious wһat ɑll іѕ neeeed too get ѕet up?
    I’m assuming having a blog ⅼike үouгs ԝould ccost
    а pretty penny? I’m not ѵery web smart sⲟ I’m not 100% ѕure.
    Anny suggestions oг advice would Ьe greatly appreciated.
    Thanks

    Feel frree to visjt my һomepage: daftar slot online

  10. agenslot777表示:

    My brother recommended I might like this blog.
    He was totally right. This post truly made my
    day. You cann’t imagine simply how much time I
    had spent for this information! Thanks!

  11. Betting表示:

    %%

    Also visit my blog :: Betting

發佈留言

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