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,466,894 Responses

  1. Chase表示:

    Hey there outstanding website! Does running a
    blog like this take a large amount of work? I’ve virtually no knowledge of
    coding however I had been hoping to start my own blog in the near future.
    Anyway, if you have any recommendations or techniques for new blog owners please share.
    I know this is off subject but I simply needed to ask.
    Many thanks!

  2. Maggie表示:

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

  3. BradleyEdino表示:

    Deneme Bonusu Veren Siteler en iyi deneme bonusu veren siteler Casino Siteleri

  4. Valuable information! Discover more at cremation services

  5. BradleyEdino表示:

    sweet bonanza slot sweet bonanza demo oyna sweet bonanza

  6. Wallacefex表示:

    casino bahis siteleri: Casino Siteleri – en guvenilir casino siteleri

  7. bandar slot表示:

    Howdy just wanted to give you a quick heads up and
    let you know a few of the pictures aren’t loading
    correctly. I’m not sure why but I think its a linking issue.
    I’ve tried it in two different internet browsers and both show
    the same results.

  8. Thanks for the thorough analysis. Find more at roofers near me

  9. Octavio表示:

    Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is important and everything. Nevertheless
    just imagine if you added some great images or videos to give your posts
    more, “pop”! Your content is excellent but with pics and
    video clips, this website could undeniably be one of the best in its niche.
    Very good blog!

  10. Spot on with this write-up, I absolutely feel this amazing site needs a great deal more attention. I’ll probably be back again to see more, thanks
    for the advice!

    My web page Small business web design

  11. Manuel Henry表示:

    Thanks for the great explanation. More info at fire damage restoration services

  12. JasonUnsoG表示:

    http://slotsiteleri25.com/# slot siteleri

  13. Wallacefex表示:

    slot casino siteleri: az parayla cok kazandiran slot oyunlar? – slot oyunlar?

  14. donat89 login表示:

    Hi there just wanted to give you a quick heads up.
    The text in your article seem to be running off the screen in Firefox.
    I’m not sure if this is a format issue or something to
    do with browser compatibility but I thought I’d post to let you know.
    The design look great though! Hope you get the issue solved soon. Many thanks

    My web site: donat89 login

  15. JasonUnsoG表示:

    https://slotsiteleri25.com/# en cok kazand?ran slot oyunlar?

  16. Thaddeus表示:

    Hello there! This post couldn’t be written any better!
    Reading through this post reminds me of my good old room mate!
    He always kept chatting about this. I will forward this write-up to him.
    Fairly certain he will have a good read. Many thanks for sharing!

  17. Annie Rowe表示:

    Brushing my canine made use of to be a task mobile dog grooming prices

  18. mzplay表示:

    Very great post. I simply stumbled upon your blog and wanted to say that I have truly loved browsing your weblog posts. In any case I?ll be subscribing to your rss feed and I hope you write again soon!

  19. Heya! I’m at work browsing your blog from my new iphone 4!
    Just wanted to say I love reading your blog and
    look forward to all your posts! Keep up the outstanding work!

  20. Myrtle Meyer表示:

    Maid services can help reduce stress in your life—who wouldn’t want that? Explore your options at house cleaning

  21. Medicament information sheet. Drug Class.
    can i get generic paxil
    Actual information about drug. Read information here.

  22. 대전유흥表示:

    Fun Different Date Ideas 대전유흥

  23. hiop表示:

    Karaoke Bar hiop

  24. Suzanne表示:

    Incontestable Evidence That You Need Car
    Accident Lawyers accident car lawyer (Suzanne)

  25. Howdy superb blog! Does running a blog similar
    to this require a large amount of work? I’ve virtually no understanding
    of coding but I was hoping to start my own blog in the near future.
    Anyhow, if you have any ideas or techniques for new blog owners please share.
    I know this is off topic but I simply had to ask. Thank you!

  26. JacobLoody表示:

    https://rlkk.ru — Все для вашего удобства: каталог, консультации и профессиональный сервис.

  27. Lloyd Coleman表示:

    Thanks for the great tips. Discover more at residential roofers Shelbyville IN

  28. top up表示:

    I’ve been an energetic user of LiveJasmin for quite
    some time currently, and I’ve checked out various ways
    to maximize my experience on the system without breaking the
    financial institution. If you are looking for ways to
    obtain free LiveJasmin credit reports quickly, I’ve
    obtained a couple of tips that might help. These suggestions are based
    upon my own experiences and what I’ve learned along the road.

發佈留言

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