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...

875,340 Responses

  1. Why Adding Vehicle Diagnostics To Your Life’s Activities Will
    Make All The Different Diagnostics Mobile

  2. Responsible For A Semi Truck Lawsuits Budget? 10 Incredible Ways To Spend Your Money Semi truck accident Lawsuit

  3. Guide To Dangerous Drugs Lawyers: The Intermediate Guide The Steps To Dangerous
    Drugs Lawyers Dangerous drugs Lawyer

  4. Ten Apps To Help Manage Your Injury Attorney injury Lawyers

  5. 11 “Faux Pas” You’re Actually Able To Do With Your Slot Competitions Best Payouts Slots

  6. RandallTaulp表示:

    buy cytotec over the counter: buy cytotec over the counter – Cytotec 200mcg price

  7. WilliamRhype表示:

    https://ciprofloxacin.tech/# cipro ciprofloxacin

  8. The Main Problem With Mesothelioma Claim And How You Can Solve It mesothelioma Litigation

  9. RandallTaulp表示:

    lisinopril oral: lisinopril price in india – lisinopril tablets for sale

  10. WilliamRhype表示:

    http://ciprofloxacin.tech/# buy ciprofloxacin over the counter

  11. You’ll Never Guess This Mesothelioma Legal Question’s
    Benefits mesothelioma legal question (0522565551.Ussoft.kr)

  12. RandallTaulp表示:

    cytotec abortion pill: cytotec abortion pill – buy misoprostol over the counter

  13. Sex-Podcast表示:

    Thanks in favor of sharing such a good thought, paragraph is fastidious, thats why i have read it completely

    Here is my web page :: Sex-Podcast

  14. I have to thank you for the efforts you’ve put in penning this site.
    I’m hoping to see the same high-grade blog posts by you later on as well.
    In fact, your creative writing abilities has motivated me to get my very own site now 😉

    My web page … gold-filled necklaces

  15. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 10 mg price

  16. door repair表示:

    10 Top Mobile Apps For Double Glazing Door
    Repairs door repair

  17. RandallTaulp表示:

    lisinopril 30: https://lisinoprilpharm.com/%5Dlisinopril – buy lisinopril 20 mg online

  18. What Is It That Makes Car Diagnostic Near Me So Popular?
    vauxhall diagnostics near me

  19. RandallTaulp表示:

    cytotec abortion pill: Misoprostol 200 mg buy online – Abortion pills online

  20. Davidtes表示:

    get generic propecia without a prescription buy propecia tablets buy propecia price

  21. 10 Things That Your Family Taught You About Examples Of Online Shopping examples of online shopping

  22. RandallTaulp表示:

    how to get nolvadex: nolvadex online – tamoxifen moa

  23. Your method of telling all in this post is truly fastidious, all
    be capable of without difficulty know it, Thanks a lot https://www.transportrbeaudet.ca/fr/drain-francais

  24. Near Me表示:

    See What Audi Replacement Key Service Near Me Tricks
    The Celebs Are Using Near Me

  25. BBFly Joyn video Downloader ist mit allen Versionen von Windows
    OS kompatibel und verlangsamt die Leistung Ihres Computers nicht.

  26. This Week’s Most Popular Stories About Treadmill Foldable Treadmill Foldable treadmills Foldable

  27. RandallTaulp表示:

    get generic propecia prices: get generic propecia without rx – cost generic propecia pill

  28. I think the admin of this web site is actually working hard for his
    web page, for the reason that here every data is quality based information.

  29. Davidtes表示:

    tamoxifen generic tamoxifen benefits tamoxifen rash

發佈留言

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