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

910,720 Responses

  1. The Best Tips You’ll Receive About Double Glazing
    Repairs Near Me double Glazing repairs near Me

  2. cheapest表示:

    You’ll Never Guess This Car Key Cut Price’s Tricks cheapest

  3. RandallTaulp表示:

    tamoxifen alternatives premenopausal: where to buy nolvadex – low dose tamoxifen

  4. Davidtes表示:

    get generic propecia pill cost of generic propecia online buying cheap propecia

  5. Are You Responsible For An Repair Timber Windows Budget?
    10 Fascinating Ways To Spend Your Money Double Glazing Repairs Near Me (Valetinowiki.Racing)

  6. The Best Advice You Can Receive About Online Store Uk Cheapest Garden Water Timer

  7. Hello, I wish for to subscribe for this website to
    obtain most recent updates, so where can i do it please help out.

    Look into my webpage … Beziehungs-Podcast

  8. RandallTaulp表示:

    cost propecia no prescription: propecia without insurance – cost cheap propecia without insurance

  9. alarm表示:

    10 Places To Find Autowatch Ghost Installers
    alarm

  10. 10 Things You Learned From Kindergarden That Will Help You
    With Upvc Window Repairs window repairs near me

  11. 7 Small Changes That Will Make A Big Difference With Your Double Glazing Repairs
    North London window lock Repair London

  12. 20 Fun Infographics About Robot Vacuums For Pet
    Hair Robot Mop Vacuum Combo

  13. Morgan表示:

    Which Is The Best Online Supermarket: 10 Things I’d Like To Have Learned
    Earlier Paintable Air Return Grill (Morgan)

  14. RandallTaulp表示:

    zestoretic price: buy zestoretic online – zestril

  15. Davidtes表示:

    get generic propecia without rx buy propecia tablets buy propecia prices

  16. 5 Killer Quora Answers On Shopping Online Uk Clothes shopping online uk clothes

  17. The Reasons Why Double Glazing Repair Near Me Is The Most Sought-After Topic
    In 2023 Double glazing Repair

  18. 125.141.133.9表示:

    The Leading Reasons Why People Perform Well At The Treadmills For Sale Industry
    treadmills for Sale near Me (125.141.133.9)

  19. WilliamRhype表示:

    http://finasteride.store/# get cheap propecia no prescription

  20. Rajaplay表示:

    hello!,I really like your writing very so much! proportion we communicate extra approximately
    your article on AOL? I need an expert on this area to
    solve my problem. May be that is you! Taking a look forward to
    look you.

  21. Ardis表示:

    An Easy-To-Follow Guide To How To Buy Clothes Online From Uk Electrical Surge Protector Tlp602 (Ardis)

  22. Davidtes表示:

    cost generic propecia online how cЙ‘n i get cheap propecia pills buying cheap propecia online

  23. 15 Things You’re Not Sure Of About Motor Vehicle Case motor vehicle accident attorney

  24. RandallTaulp表示:

    ciprofloxacin over the counter: ciprofloxacin 500mg buy online – buy ciprofloxacin over the counter

  25. 10 Healthy Habits For Adult Adhd Treatment treatment of adult adhd

  26. Davidtes表示:

    buy cipro online without prescription buy cipro online cipro for sale

  27. Davidtes表示:

    lisinopril 5mg buy lisinopril 20 mg tabs lisinopril 5 mg uk price

  28. Your mode of telling all in this article is genuinely pleasant, all be able to easily know it,
    Thanks a lot.

  29. What’s The Job Market For American-Style Fridge Professionals?
    american-style Fridge

發佈留言

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