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,145,253 Responses

  1. wildfire case表示:

    15 Gifts For The CSGO Most Profitable Cases Lover In Your Life wildfire case

  2. bokep hd表示:

    My brother suggested I would possibly like this web site.
    He was once totally right. This submit truly made my day.
    You can not believe just how a lot time I had spent for this
    information! Thank you!

  3. What’s The Job Market For Window Doctor Near Me Professionals?
    Window Doctor near me

  4. Slot symbols表示:

    Ten Real Money Slots That Will Help You Live Better Slot symbols

  5. 16 Must-Follow Pages On Facebook For Cheap Online
    Shopping Sites Uk Marketers Best headache Rack

  6. The Little-Known Benefits Of Link Building Software
    Best Backlink Builder Software

  7. near Me表示:

    5 People You Oughta Know In The Door Repairs London Industry near Me

  8. What’s The Current Job Market For Best Rated Folding Treadmill Professionals?
    best rated folding treadmill

  9. 9 . What Your Parents Teach You About France
    Online Shopping Sites Clothes france online shopping sites clothes

  10. Shadow case表示:

    The Ultimate Cheat Sheet For What CSGO Cases Should I Open Shadow case

  11. Hi there! I could have sworn I’ve been to this website before but after going through many of the posts I
    realized it’s new to me. Anyhow, I’m definitely delighted I found it
    and I’ll be book-marking it and checking back often!

  12. www.fjclwz.us表示:

    A Brief History Of Mobile Slots History Of Mobile
    Slots Top software providers for slots; http://www.fjclwz.us,

  13. 9 Things Your Parents Teach You About Fela Accident Attorney fela Accident attorney

  14. Launa表示:

    The 10 Scariest Things About Best Slots best slots (Launa)

  15. 9 Lessons Your Parents Teach You About Slot Strategies Slot Strategies

  16. 14 Misconceptions Commonly Held About Motorcycle Accident
    Legal Motorcycle Accident Lawyers

  17. 10 Things You Learned In Preschool That Will Help You
    With Car Accident Attorney Car Accident lawsuit

  18. You really make it appear really easy together with your presentation however I find this matter to be really something which I feel I’d by no means understand. It kind of feels too complicated and very vast for me. I’m having a look forward for your subsequent submit, I will try to get the hang of it!

  19. I blog frequently and I really thank you for your content.
    This article has truly peaked my interest. I am going to bookmark your blog and keep checking for new details about once per week.
    I opted in for your RSS feed too.

  20. How To Resolve Issues With Slot Variance Low variance Slots

  21. The 10 Most Terrifying Things About Medium Variance Slots Medium variance slots

  22. Why Mercedes Key Fob Still Matters In 2023 genuine Mercedes key fob

  23. 匿名訪客表示:

    Действительно….такой полезный онлайн сайт. Посетите также мою страничку https://www.printwhatyoulike.com/get_page?topic=59750.100&url=https://migration-bt4.co.uk/profile.php?id=400078

  24. The 10 Most Scariest Things About Classic Casino Slots classic casino slots

  25. 8 Tips To Improve Your Ferrari Car Key Replacement Game ferrari Locksmith near Me

  26. I’m curious to find out what blog platform you happen to be utilizing?
    I’m experiencing some minor security issues with my latest
    website and I’d like to find something more risk-free.
    Do you have any suggestions?

發佈留言

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