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,478,471 Responses

  1. Markdob表示:

    Приветствую. Может кто знает, где найтиразные блоги о недвижимости? Пока нашел – https://unnaty.ru

  2. 17 Signs To Know You Work With Fireplace Wall Mount wall fire place (http://www.google.com.gi)

  3. 15 Presents For The Spare Van Keys Lover In Your Life How Much For Spare Car Key

  4. Reuben表示:

    How To Make An Amazing Instagram Video About Shop Anal Toy best male anal
    toys (Reuben)

  5. Meds information. Short-Term Effects.
    can i purchase generic clomid no prescription
    All information about medicine. Read here.

  6. Markdob表示:

    Всем привет! Может кто знает, где найтиполезные статьи о недвижимости? Пока нашел – https://unnaty.ru

  7. BrandenSlomy表示:

    amoxicillin 500mg capsule: order amoxicillin online – buying amoxicillin online

  8. Many cell caps simply screw in position, so you Can AGM golf cart batteries be restored (http://pirooztak.ir/)
    eliminate them in this way.

  9. bscbs.co.kr表示:

    Hi there, You’ve done an excellent job. I will certainly
    digg it and personally suggest to my friends. I am sure they will be benefited from this site.

  10. macauslot88表示:

    I am sure this paragraph has touched all the internet visitors, its really really good article on building up new blog.

  11. Nikefut表示:

    bitcoin dark web dark markets 2024 tor market links

  12. Arper meubels表示:

    I would like to thank you for the efforts you’ve put in penning this
    blog. I am hoping to see the same high-grade content by you
    in the future as well. In fact, your creative writing abilities has
    inspired me to get my very own blog now 😉

  13. Eduardo表示:

    Really when someone doesn’t know after that its up to
    other users that they will assist, so here it occurs.

  14. Meds information sheet. Effects of Drug Abuse.
    is benicar the same as lisinopril
    Best news about meds. Read now.

  15. The 10 Scariest Things About Car Keys Spare car key copy near Me

  16. AlbertDuM表示:

    generic plavix: Clopidogrel Best Prices – clopidogrel bisulfate 75 mg

  17. Wow, incredible blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is fantastic, as well as the content!

  18. AlbertDuM表示:

    order cytotec online: buy cytotec online – buy cytotec in usa

  19. AlbertDuM表示:

    minocycline for rosacea: buy Stromectol – stromectol tablet 3 mg

  20. вывод из запоя в стационаре анонимно https://zal.rolevaya.info/viewtopic.php?id=5384 .

  21. DarrenUnjuh表示:

    https://plavixclo.com/# Cost of Plavix on Medicare

  22. JacobLoody表示:

    кухни москва — Стильные и функциональные кухни для московских квартир.

  23. I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my opinion, if all
    webmasters and bloggers made good content as you did, the web will be
    much more useful than ever before.

  24. Hey There. I discovered your blog the use of
    msn. That is a really well written article. I’ll make
    sure to bookmark it and come back to read more
    of your helpful info. Thanks for the post. I’ll certainly
    return.

  25. Unblocked表示:

    Asking questions are actually good thing if you are not understanding anything fully, however
    this piece of writing gives fastidious understanding even.

發佈留言

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