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,235,784 Responses

  1. RandallTaulp表示:

    raloxifene vs tamoxifen: tamoxifen 20 mg – tamoxifen and ovarian cancer

  2. Davidtes表示:

    buy cytotec in usa Cytotec 200mcg price buy cytotec over the counter

  3. WilliamRhype表示:

    https://ciprofloxacin.tech/# ciprofloxacin generic price

  4. Guide To Garden Wood Burner: The Intermediate Guide Towards Garden Wood Burner garden wood Burner

  5. WilliamRhype表示:

    http://nolvadex.life/# natural alternatives to tamoxifen

  6. Visit表示:

    Howdy! I know this is kinda off topic but I was wondering
    if you knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having trouble finding one?
    Thanks a lot!

  7. nirwana88表示:

    Ѕіmply wish to say youг aгticle is as astounding.
    The clarity to yoսr post is just spectacսlar and thawt i could
    suppose you are a professionaⅼ in this subject.

    Well along with your permission let me to grasp үour RSS feе to keep ᥙpdated
    with impending post. Thank you 1,000,000 and please continu the grаtifying work.

    Also ѵіsit my page nirwana88

  8. Why All The Fuss About Boat Accident Lawyers?

    Boat Accident Law Firm

  9. jamuslot表示:

    Evеryone lοveѕ what you guys tend to be up too.
    This sort օօf clever ork and reporting! Keep up the good works ghyѕ I’ve you
    guys to our blogroll.

    my bⅼog posst – jamuslot

  10. 10 Apps To Aid You Manage Your Replacement Windows Prices
    windows Replacement

  11. This website was… how do you say it? Relevant!! Finally
    I’ve found something which helped me. Many thanks!

  12. 11 Creative Methods To Write About American Freezer Fridge american integrated fridge freezer

  13. 10 Tell-Tale Signals You Should Know To Look For A
    New CSGO Cases Highest Roi Cs20 case (maps.google.Com.sa)

  14. The 10 Most Terrifying Things About Double Glazing
    Replacement Window Double glazing replacement Window

  15. RandallTaulp表示:

    cytotec online: buy cytotec pills – order cytotec online

  16. RandallTaulp表示:

    ciprofloxacin mail online: ciprofloxacin mail online – buy ciprofloxacin

  17. The 10 Most Scariest Things About Personal
    Injury Attorneys personal injury attorney

  18. The Greatest Sources Of Inspiration Of Replacement Windows Near Me Windows Replacement

  19. Porsche key表示:

    10 Things That Your Family Taught You About
    Porsche Key Fobs Porsche key

  20. sash表示:

    What Is Windows Repair Near Me? And How To Make Use Of It sash

  21. 10 Websites To Help You To Become An Expert In Private ADHD
    Assessment Cost how much does A private adhd assessment cost

  22. Vimeo表示:

    The Reason Top 10 Online Shopping Sites In Uk For
    Clothes Is The Obsession Of Everyone In 2023 Vimeo

  23. Davidtes表示:

    tamoxifen joint pain nolvadex during cycle tamoxifen moa

  24. RandallTaulp表示:

    buy generic ciprofloxacin: ciprofloxacin generic price – antibiotics cipro

  25. I for all time emailed this weblog post page to all my associates, because if like to read it then my friends will too.

  26. Services Seo表示:

    The 12 Most Popular Buy SEO Services Accounts To Follow On Twitter Services Seo

  27. RandallTaulp表示:

    clomid nolvadex: does tamoxifen make you tired – what is tamoxifen used for

  28. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen 20 mg

  29. WilliamRhype表示:

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

  30. You’ll Never Guess This Replacement Windows
    Near Me’s Secrets replacement windows near me

發佈留言

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