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,674,576 Responses

  1. Hello very cool blog!! Guy .. Beautiful .. Superb .. I will bookmark your website and take the feeds also?
    I am happy to seek out a lot of useful information here in the submit, we need develop more
    strategies in this regard, thank you for sharing.
    . . . . . http://gaziantep.ogo.org.tr/question/meubles-en-coin-de-cuisine-optimisation-de-lespace-et-esthetique-7/

  2. Warm XXX black ladies having sex, damn warm!.

  3. Wonderful site you have here but I was curious about if you
    knew of any discussion boards that cover the same topics talked about in this article?

    I’d really love to be a part of online community where I can get responses from other experienced individuals that share
    the same interest. If you have any recommendations, please
    let me know. Thanks!

  4. Wow! This blog looks just like my old one! It’s on a completely different subject but it has pretty much the same
    layout and design. Superb choice of colors!

  5. Power Trades表示:

    That is a great tip especially to those new to the blogosphere.
    Short but very precise info… Appreciate your sharing this
    one. A must read post!

  6. mostbet_ntKi表示:

    мостбет скачать андроид http://mostbet5003.ru/ .

  7. CarltonUnith表示:

    http://usaindiapharm.com/# USA India Pharm

  8. Philipitags表示:

    https://vintageamericanapodcast.com/ – Discover America’s vintage heritage through engaging storytelling

  9. Warm and balmy sex adventures in India? Yes please!

  10. DavidSooni表示:

    UsaIndiaPharm USA India Pharm UsaIndiaPharm

  11. FrankGlora表示:

    UsaIndiaPharm: USA India Pharm – USA India Pharm

  12. Very warm! Bollywood actress XXX is on fire!.

  13. When someone writes an paragraph he/she keeps the image of a user in his/her brain that how a user
    can know it. So that’s why this piece of writing is perfect.
    Thanks!

  14. Drugs prescribing information. Effects of Drug Abuse.
    mirtazapine 15 mg tablet tev
    Actual trends of drugs. Read information now.

  15. 匿名訪客表示:

    Terima kasih atas informasinya yang sangat bermanfaat.
    Kebetulan saya juga menemukan informasi menarik di ultra4dtoto yang mungkin bisa melengkapi pembahasan ini.

  16. Hot BF? A Lot More like Warm Mess! (bf attractive warm).

  17. CarltonUnith表示:

    https://usaindiapharm.com/# indian pharmacy

  18. 1win_emPt表示:

    один вин 1win7016.ru .

  19. PUBLINANAR表示:

    Do you mind if I quote a couple of your articles as long as I provide credit and sources
    back to your webpage? My blog site is in the exact same area of interest
    as yours and my visitors would truly benefit from a lot of the information you provide here.
    Please let me know if this alright with you. Cheers!

  20. CarltonUnith表示:

    https://usaindiapharm.shop/# Online medicine order

  21. Hot scene, warm scene! Gay BDSM porn at its finest.

  22. Georgeunula表示:

    USA India Pharm: USA India Pharm – pharmacy website india

發佈回覆給「PUBLINANAR」的留言 取消回覆

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