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,229,970 Responses

  1. Gerard表示:

    Double Glazing Repair Near Me: The Ugly Real Truth Of Double Glazing
    Repair Near Me double glazing window handle (Gerard)

  2. Lost表示:

    15 Reasons You Must Love Car Key Replacement Audi Lost

  3. windows表示:

    Could Repair Double Glazed Window Be The Key To 2023’s Resolving?
    windows

  4. Ronaldpsymn表示:

    http://finasteride.store/# cost propecia without dr prescription

  5. Davidtes表示:

    cytotec pills buy online Abortion pills online cytotec online

  6. Truck Crash Lawyer: The Ultimate Guide To Truck Crash Lawyer Truck Accidents

  7. Www.google.at表示:

    13 Things About CSGO Weapon Case You May Not Know esports 2014 Summer case
    http://Www.google.at

  8. Don’t Make This Silly Mistake You’re Using Your Slot Competitions
    Slots with free Spins; http://www.plantsg.com.sg,

  9. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 5 mg tablet price

  10. The 10 Most Terrifying Things About Upvc Window Repairs Upvc Window Repairs Near Me

  11. Davidtes表示:

    medicine lisinopril 10 mg lisinopril 10 mg order online buy lisinopril without a prescription

  12. WilliamRhype表示:

    https://cytotec.club/# buy cytotec online fast delivery

  13. Ulrike表示:

    12 Companies Are Leading The Way In Why Are CSGO Keys So Expensive case spectrum – Ulrike

  14. Assessing Mental Health Tools To Make Your Daily Lifethe One Assessing Mental Health Trick Every Individual Should Know assessing Mental health

  15. WATER表示:

    I have fun with, result in I found just what I was looking
    for. You’ve ended my 4 day long hunt! God Bless you man. Have a
    nice day. Bye

  16. coin auctions表示:

    Excellent post however I was wanting to know if you could write a litte more on this subject?
    I’d be very grateful if you could elaborate a little bit further.
    Appreciate it!

    Feel free to visit my blog coin auctions

  17. Some Of The Most Common Mistakes People Make With List
    Of Online Shopping Sites In Uk Timbuk2 Backpack Deals

  18. Mesothelioma Claim Tools To Simplify Your Daily Life Asbestos Litigation

  19. Hey! Quick question that’s completely off topic. Do you know
    how to make your site mobile friendly? My blog looks weird when viewing from my apple iphone.
    I’m trying to find a theme or plugin that might be able to correct this issue.
    If you have any recommendations, please share.
    With thanks!

  20. Indiana表示:

    5 Qualities People Are Looking For In Every Programming Car
    Keys car key reprogrammed (Indiana)

  21. You’ll Never Be Able To Figure Out This Locksmith Automotive Near Me’s Tricks Locksmith Automotive

  22. Christopher表示:

    15 Gifts For The Private ADHD Lover In Your Life can i get a private adhd assessment (Christopher)

  23. RandallTaulp表示:

    get propecia: buy cheap propecia tablets – generic propecia without rx

  24. 10 Things We All Are Hating About 3 Wheel Mobility Scooters
    Near Me three wheeled mobility Scooters uk

  25. Adell表示:

    5 CSGO Case Battle Projects That Work For
    Any Budget case operation bravo – Adell

  26. Davidtes表示:

    zestril tablet price lisinopril 20 mg pill lisinopril online prescription

  27. 5 Killer Quora Answers To ADHD Titration UK adhd titration uk – cheaperseeker.com,

  28. film porno表示:

    Hi, I do believe this is a great website. I stumbledupon it 😉 I am going to
    return yet again since I book-marked it. Money and freedom is the best way
    to change, may you be rich and continue to guide other people.

  29. Places To Buy Electric Scooters Near Me Tips From The Top In The Industry best boot mobility scooter uk

  30. The 10 Scariest Things About Free Spin Slots Free Spin Slots

發佈留言

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