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,477,159 Responses

  1. Violette Nies表示:

    7 Tips To Make The Most Out Of Your Toto Sites 안전놀이터 추천

  2. Howdy! I could have sworn I’ve been to this website before but after
    reading through some of the post I realized it’s new to me.
    Anyways, I’m definitely glad I found it and I’ll be bookmarking
    and checking back often!

  3. Josette Fitts表示:

    Everything You Need To Know About Car Seat For Newborn Infant car seat

  4. Elliot Scarf表示:

    10 Reasons That People Are Hateful To Situs Togel Terpercaya Situs Togel Terpercaya Situs toto togel

  5. Karen Trahan表示:

    The 10 Most Terrifying Things About Spare Key For My Car Spare key For my car

  6. Профессиональный сервисный центр отремонтировать телефон ремонт сотовых телефонов рядом

  7. Tier Links 101 The Ultimate Guide For Beginners tier 2 backlinks

  8. Florida Zepps表示:

    The 9 Things Your Parents Taught You About Situs Togel Terpercaya situs togel terpercaya

  9. Nine Things That Your Parent Teach You About Double Glazed Windows Handles Double Glazed Windows handles

  10. A knowledgeable asbestos attorney will assist you in filing an official claim to receive compensation for your suffering.You can recover damages through filing a workers’ compensation or personal injury lawsuit.

  11. BrandonJer表示:

    drug medication buy online pharmacy legal to buy prescription drugs from canada

  12. AgustinBougs表示:

    https://mexicanpharm24.pro/# pharmacies in mexico that ship to usa
    п»їlegitimate online pharmacies india

  13. Greetings! This is my first visit to your blog! We are
    a team of volunteers and starting a new initiative in a community in the
    same niche. Your blog provided us valuable information to work on. You have done a outstanding job!

  14. A reliable law firm that is specialized in mesothelioma may assist families and victims to receive compensation. Mesothelioma lawyers are experts in asbestos case (Dino) litigation and can aid victims with trust fund claims, lawsuits,or VA benefits.

  15. Need Inspiration? Try Looking Up Double Glazed
    Replacement Glass Near Me double glazing near Me (https://Ejlersen-cohen.hubstack.net)

  16. Awinitadox表示:

    Fantastic info. Appreciate it!
    real online cash casinos free play online casino watch movie casino 1995 online free

  17. Tesha Hepler表示:

    How To Outsmart Your Boss On Porsche Macan Key porsche boxster key replacement cost

  18. AgustinBougs表示:

    http://indianpharmdelivery.com/# india pharmacy
    best online pharmacy india

  19. Why People Don’t Care About Collapsible Scooter Collapsible Scooter Mobility – Images.Google.Cg

  20. BrandonJer表示:

    reputable mexican pharmacies online mexico drug stores pharmacies mexico pharmacies prescription drugs

  21. Профессиональный сервисный центр сервисный центр телефонов ремонт сотовых

  22. Experience seamless gaming on taya365, where top-notch entertainment meets exciting prizes.

  23. Could Espresso Coffee Maker Be The Answer To Dealing With 2023?Espresso Coffee Machine

  24. Sofia Wesley表示:

    5. Mazda Keys Replacement Projects For Any Budget mazda spare Key, glamorouslengths.com,

  25. Give the gift card over to a customer service rep or cashier, and ask them to determine the balance on the present card. They are usually capable of scanning the card and let you know the balance.

  26. Why Asbestos Mesothelioma Isn’t A Topic That People Are Interested In Asbestos Mesothelioma Asbestos lawyers

  27. Everything you need about the Nordstrom credit card is in your reach. You can activate the card Manage your account, pay for purchases, check your Rewards, and much more!

  28. BrandonJer表示:

    mexican border pharmacies shipping to usa medicine in mexico pharmacies mexican mail order pharmacies

發佈留言

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