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,668,978 Responses

  1. New Audi Keys表示:

    Why Adding A Audi A3 Key Replacement To Your Life Will
    Make All The Change New Audi Keys

  2. Jorgeagopy表示:

    india pharmacy mail order: Indian pharmacy international shipping – reputable indian pharmacies

  3. капельница при алкогольной интоксикации на дому цена капельница при алкогольной интоксикации на дому цена .

  4. Aaronrem表示:

    Качественное и быстрое строительство домов, узнать больше можно здесь https://pastelink.net/1xe5gipz

  5. вывод из запоя с выездом королев https://vyvod-iz-zapoya-korolev12.ru/ .

  6. hoangvuongtai表示:

    I do not even understand how I finished up here, but
    I thought this post used to be good. I don’t recognize who you might be but definitely you are going to a famous
    blogger in case you aren’t already. Cheers!

  7. LeroyBak表示:

    buying prescription drugs in mexico online mexico pharmacy mexican drugstore online

  8. Clarkwer表示:

    Отечественный магазин НашаМебель зовет широченный прибор кухонь, которые посодействуют сделать устроенность и еще благоустроенность в вашем жилище https://www.notahye4kuhnishki.ru/.

  9. Meds information sheet. Brand names.
    pantoprazole fda approval
    All about drugs. Get information here.

  10. 10 Things That Your Family Taught You About Togel4d Login togel4d login (https://situs-terpercaya94528.blogars.com/)

  11. What’s The Current Job Market For Fabric Two Seater Sofa Professionals Like?
    fabric two Seater Sofa

  12. BrandonSaisy表示:

    indian pharmacy online: medicines from India – pharmacy website india

  13. See What Single Serve Coffee And Espresso Maker Tricks The Celebs Are Using single serve coffee and espresso maker

  14. вызвать капельнцу от запоя kapelnica-ot-zapoya-podolsk11.ru .

  15. 10 Misconceptions Your Boss Holds Concerning Wall Mounted Electric Fires
    Wall mounted Fires

  16. LeroyBak表示:

    mexican mail order pharmacies Legit online Mexican pharmacy medicine in mexico pharmacies

  17. I do accept as true with all the ideas you’ve introduced on your post.

    They are really convincing and can definitely work.
    Still, the posts are too quick for starters. May you please lengthen them a little from subsequent
    time? Thanks for the post.

  18. slot gacor表示:

    I am in fact grateful to the holder of this site who has shared this fantastic article at at
    this time.

  19. It’s a shame you don’t have a donate button! I’d certainly donate to this superb
    blog! I guess for now i’ll settle for book-marking and adding
    your RSS feed to my Google account. I look forward to fresh updates and will
    share this site with my Facebook group. Talk soon!

  20. DevonPhack表示:

    http://pharm24.pro/# prescription meds without the prescriptions
    how to overcome ed naturally

  21. What’s The Reason Everyone Is Talking About Locksmiths
    Near Me This Moment Emergency locksmiths near me

  22. LeroyBak表示:

    top online pharmacy india Indian pharmacy online online shopping pharmacy india

  23. Jorgeagopy表示:

    buying prescription drugs in mexico: mexican drugs – purple pharmacy mexico price list

發佈回覆給「vivod iz zapoya korolev_daEt」的留言 取消回覆

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