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,449,154 Responses

  1. It’s genuinely very complicated in this busy life to listen news
    on TV, therefore I simply use internet for that reason, and get the most recent news.

  2. Jorgewooff表示:

    Mexican Easy Pharm: Mexican Easy Pharm – medication from mexico pharmacy

  3. Jorgewooff表示:

    Mexican Easy Pharm: mexican online pharmacies prescription drugs – best online pharmacies in mexico

  4. Nine Things That Your Parent Teach You About Best Capsule
    Coffee Machine Best capsule Coffee Machine

  5. Some Of The Most Ingenious Things That Are Happening With Asbestos Mesothelioma Compensation mesothelioma lawsuits
    (http://Bbs.tejiegm.Com/)

  6. kypit 1s_zdor表示:

    1с бухгалтерия купить 1с бухгалтерия купить .

  7. Brandonnot表示:

    canadian drug prices: CanadianMdPharm – canada drugs reviews

  8. Hi to every body, it’s my first visit of this webpage; this website
    consists of amazing and genuinely excellent stuff designed for readers.

  9. JacobLoody表示:

    http://www.pravoslavieug.ru — Полный каталог кухонь и услуг доступен на сайте pravoslavieug.

  10. Brandonnot表示:

    safe reliable canadian pharmacy: is canadian pharmacy legit – rate canadian pharmacies

  11. AndreDib表示:

    legal canadian pharmacy online https://indiancertpharm.com/# indian pharmacy
    indian pharmacies safe

  12. вывод из запоя в стационаре воронежа http://www.vyvod-iz-zapoya-v-stacionare-voronezh23.ru .

  13. CurtisBam表示:

    safe canadian pharmacies: Canadian Md Pharm – buying drugs from canada

  14. JacobLoody表示:

    http://pravoslavieug.ru — Узнайте, как легко заказать кухню на сайте pravoslavieug.

  15. Don表示:

    10 Things People Get Wrong About Black Friday Power Tool Deals buy power tools online uk (Don)

  16. AndreDib表示:

    medication canadian pharmacy https://indiancertpharm.com/# Indian pharmacy international shipping
    online shopping pharmacy india

  17. kypit 1s_efEn表示:

    настроить 1с бухгалтерия цена настроить 1с бухгалтерия цена .

  18. See What Buy Real Driving License UK Tricks The Celebs Are Utilizing buy real driving license uk

  19. Donald表示:

    Awesome! Its truly amazing paragraph, I have got much clear idea regarding
    from this paragraph. http://Forum.Altaycoins.com/profile.php?id=1041291

  20. kypit 1s_apEn表示:

    купить 1с бухгалтерия 8.3 проф версия цена купить 1с бухгалтерия 8.3 проф версия цена .

  21. Brandonnot表示:

    buying from online mexican pharmacy: mexico drug stores pharmacies – reputable mexican pharmacies online

  22. kypit 1s_pvEn表示:

    программы фирмы 1с программы фирмы 1с .

  23. Hi there would you mind letting me know which hosting company
    you’re utilizing? I’ve loaded your blog in 3 completely different browsers and I must say this blog loads a lot quicker then most.

    Can you recommend a good internet hosting provider at a honest
    price? Thanks, I appreciate it!

  24. With havin so much content do you ever run into any issues of plagorism or copyright infringement?

    My blog has a lot of unique content I’ve either created myself
    or outsourced but it looks like a lot of it is popping it up all over the internet without my agreement.
    Do you know any solutions to help prevent content from being ripped off?
    I’d certainly appreciate it.

  25. Medicament information leaflet. Drug Class.
    can you get singulair without rx
    Everything information about pills. Get now.

  26. kypit 1s_ciEn表示:

    настроить 1с бухгалтерия цена https://svarog.forum24.ru/?1-0-0-00000330-000-0-0 .

  27. Carrolleruck表示:

    https://indiancertpharm.shop/# Indian pharmacy international shipping
    reputable canadian pharmacy

發佈留言

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