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,366,890 Responses

  1. Medicine information. What side effects can this medication cause?
    where to get aricept without prescription
    Best trends of medication. Read information here.

  2. Good day! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m undoubtedly enjoying your blog and look forward to new updates.

  3. карнизы для штор с электроприводом карнизы для штор с электроприводом .

  4. Wow, this article is pleasant, my sister is analyzing
    these things, so I am going to let know her.

    Also visit my web blog :: Copper peptide serum

  5. IronRods表示:

    you’re truly a excellent webmaster. The web site loading pace is
    amazing. It sort of feels that you’re doing any unique trick.
    Moreover, The contents are masterwork. you have done a great
    activity on this matter!

    Also visit my blog post – IronRods

  6. Nikefut表示:

    darknet markets 2024 dark web link https://darknetmarketstore.com/ – tor market url

  7. карнизы с электроприводом карнизы с электроприводом .

  8. BrandenSlomy表示:

    cheap priligy: max pharm – dapoxetine online

  9. regardless from that, choose whether you are a classic slot, a rich farm or fighting the undead,
    you are waiting fascinating https://sweet-bonanza-uk.org/!

  10. Hello, yeah this post is genuinely fastidious and I have learned lot of
    things from it about blogging. thanks.

    Also visit my homepage – walet69 link alternatif

  11. Dereketerm表示:

    max pharm: max pharm – Priligy tablets

  12. BrandenSlomy表示:

    order prednisone from canada: raypharm – buy prednisone from canada

  13. BrandenSlomy表示:

    buy priligy: dapoxetine price – priligy

  14. Blakesmema表示:

    http://www.webcocktail.ru — Наш сайт предлагает разнообразие кухонь на заказ для любого бюджета.

  15. Hi! This is kind of off topic but I need some guidance from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about creating my own but I’m not sure where to begin. Do you
    have any points or suggestions? Cheers

    Here is my blog :: omegle alternatives

  16. Nikefut表示:

    dark market list darkmarket list https://darknetmarketstore.com/ – darknet market list

  17. Excellent, what a weblog it is! This weblog presents helpful data to
    us, keep it up.

    Visit my page :: Best accident lawyers in Phoenix

發佈留言

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