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,366,980 Responses

  1. Frankevild表示:

    https://ozempic.art/# ozempic generic

  2. Excellent web site you have got here.. It’s hard to
    find quality writing like yours these days. I truly appreciate people like you!
    Take care!!

  3. Adolfo表示:

    How Handles And Hinges Became The Hottest Trend Of 2024 glass window accessories friction stay hinge [Adolfo]

  4. Great blog here! Also your website loads up very fast!
    What host are you using? Can I get your affiliate
    link to your host? I wish my website loaded up as fast as yours lol

  5. 5 Must-Know Hismphash Practices You Need To Know For 2023 private adhd assessment bristol

  6. 먹튀검증表示:

    Do You Think Sport Toto Result Today One Day Rule The World?
    먹튀검증

  7. BarryDof表示:

    http://rybelsus.shop/# rybelsus cost

  8. HaroldBig表示:

    buy ozempic: ozempic cost – buy ozempic

  9. ai-db.science表示:

    You’ll Never Guess This Upvc Door Doctor Near Me’s Secrets door doctor Near me (ai-db.science)

  10. HaroldBig表示:

    ozempic: ozempic cost – ozempic coupon

  11. The last-observation-carried-forward method was used to estimate missing efficiency
    information.

  12. RamonRek表示:

    buy ozempic: buy ozempic pills online – buy cheap ozempic

  13. BarryDof表示:

    https://rybelsus.shop/# semaglutide tablets

  14. gocengqq info表示:

    Just want to say your article is as surprising. The clarity for your post is just excellent and that i could think you’re a professional
    on this subject. Fine together with your permission let
    me to grab your RSS feed to keep up to date with
    coming near near post. Thanks one million and please carry on the rewarding work.

  15. Jeremypag表示:

    cheapest rybelsus pills semaglutide cost buy semaglutide online

  16. Genuinely when someone doesn’t know afterward its up
    to other users that they will assist, so here it happens.

  17. Basil表示:

    For these factors, any type of off-label or nonprescription use of GHSs is not secure.

  18. Frankevild表示:

    https://ozempic.art/# Ozempic without insurance

  19. Inviertas表示:

    We are a group of volunteers and opening a new scheme in our community.
    Your web site offered us with valuable info to work on. You’ve done a formidable job and our whole community will be thankful to
    you.

    Have a look at my web blog :: Inviertas

  20. This paragraph is in fact a nice one it helps new the web viewers,
    who are wishing in favor of blogging.

  21. 5 Killer Quora Answers On Replacement Key For Audi
    A3 replacement key for audi A3

  22. BarryDof表示:

    https://rybelsus.shop/# cheapest rybelsus pills

  23. HaroldBig表示:

    semaglutide tablets: buy semaglutide pills – semaglutide tablets

  24. Intelligence表示:

    Hi mates, fastidious post and fastidious urging commented at this place,
    I am genuinely enjoying by these.

  25. Jeremypag表示:

    buy cheap ozempic ozempic cost buy ozempic pills online

  26. WilliamDeeli表示:

    Здравствуйте!
    Мы предлагаем дипломы.
    msobl.flybb.ru/posting.php?mode=post&f=2

  27. Thank you a lot for sharing this with all folks you actually recognise what you
    are talking approximately! Bookmarked. Please additionally consult with my website =).
    We can have a hyperlink trade arrangement among us

    Review my website – Мега магазин

  28. Wow that was strange. I just wrote an extremely 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 superb
    blog!

發佈留言

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