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,154,973 Responses

  1. 789nexia表示:

    Greetings! Very helpful advice within this article! It’s the little
    changes that will make the most significant changes.
    Many thanks for sharing!

  2. It’s an remarkable piece of writing in favor of all the online viewers; they will take benefit from it I am sure.

  3. 10 Things You Learned In Preschool That’ll Help You Understand Couches Near Me For Sale Cheap Couches For Sale Under $200

  4. 10 Things We Are Hating About Motor Vehicle Legal motor Vehicle Accident

  5. Excellent goods from you, man. I’ve understand your stuff previous to and you’re just extremely excellent.
    I really like what you’ve acquired here, certainly like what you’re stating and the way in which you say it.
    You make it enjoyable and you still care for to keep it sensible.
    I can not wait to read far more from you. This is actually a tremendous site.

  6. Slot Gacor表示:

    Greetings! I ҝnow thіѕ is kіnd of off topic Ƅut I ᴡɑs wondering іf yyou knew
    wheге I cߋuld get a captchua plgin fοr
    my cօmment form? І’m using the same blog platform аs yoᥙrs and I’m havіng pгoblems finding οne?

    Thanks a lot!

    Feel free to visit my webpage – Slot Gacor

  7. Where Is Green Power Mobility Scooters One Year From Now? green power mobility scooter reviews

  8. Five Killer Quora Answers To Popular Casino Slots popular casino slots

  9. fun game表示:

    With havin so much content and articles do
    you ever run into any issues of plagorism or copyright infringement?
    My site has a lot of unique content I’ve either written myself or outsourced but it
    seems a lot of it is popping it up all over the internet
    without my permission. Do you know any solutions to help prevent content from being stolen? I’d really
    appreciate it.

  10. Arleen表示:

    Treatment For ADD Isn’t As Difficult As You Think adhd
    treatment london, Arleen,

  11. Five People You Need To Know In The Online Casino Slots Industry
    Top winning slots

  12. Arlen Nizo表示:

    15 Boot Mobility Scooters Benefits You Should All
    Know Arlen Nizo

  13. After a long struggle and a long period of legal action,
    asbestos legal measures resulted in the partial ban of 1989 on the manufacturing,
    processing, and distribution of a majority of Asbestos compensation-containing products.
    This ban remains in effect.

  14. Hubert表示:

    Ten Things You Learned In Kindergarden That Will Help You Get Window Companies Bedford flush casement window bedfordshire (Hubert)

  15. 5 Killer Quora Answers On Window Replacement Near Me
    window replacement near me

  16. p3terx.com表示:

    10 Tell-Tale Warning Signs You Should Know To
    Get A New Unique Slots high rtp slots (p3terx.com)

  17. Wow that was odd. I just wrote an very long comment but after I clicked
    submit my comment didn’t show up. Grrrr… well I’m not writing all that over again.
    Anyway, just wanted to say great blog!

  18. 10 Things That Your Family Taught You About
    Upvc Window Repairs Near Me upvc window repairs Near me

  19. The 10 Most Terrifying Things About Hire Truck Accident
    Lawyer truck accident

  20. The 10 Most Scariest Things About Dangerous Drugs
    Lawyers dangerous drugs lawyer

  21. Beware Of This Common Mistake With Your Sofas Leather genuine leather sofa (image.google.sh)

  22. Who’s The World’s Top Expert On 4 Wheel Disability Scooter?
    4 wheel enclosed mobility Scooter

  23. 8 Tips To Up Your Motor Vehicle Lawsuit Game Motor vehicle Accident lawsuit

  24. An In-Depth Look Into The Future What Will The Auto Accident Lawyer Industry Look Like
    In 10 Years? auto accident law firms

  25. Felipa表示:

    See What Can I Buy From A Uk Website Tricks The Celebs Are Utilizing can i buy from a uk
    website (Felipa)

  26. 10 Things That Your Family Taught You About France Online Shopping Sites Clothes
    France Online Shopping Sites Clothes (https://The-Challenger.Ru/Goto/AHR0CHM6Ly92AW1Lby5Jb20VOTMxODczODg3)

發佈留言

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