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

911,106 Responses

  1. Dennisscalp表示:

    https://viagracanada.xyz/# does viagra stop you coming

  2. Hi! I know this is kind of off topic but I was wondering if
    you knew where I could find a captcha plugin for my comment form?

    I’m using the same blog platform as yours and I’m having trouble finding one?
    Thanks a lot!

  3. Excellent post. I was checking continuously this weblog and I
    am inspired! Extremely useful information specially the remaining section :
    ) I deal with such information a lot. I was looking for this certain info for a very long time.
    Thank you and best of luck.

  4. This post will help the internet visitors for building
    up new blog or even a blog from start to end.

  5. VernonBuM表示:

    nolvadex for pct tamoxifen and osteoporosis

  6. I love this website – its so usefull and helpfull.

    https://www.kopalniapracy.pl

  7. w88表示:

    This article will assist the internet people for setting up
    new blog or even a blog from start to end.

    Also visit my web-site w88

  8. Spot on with this write-up, I truly believe that this amazing site needs far more attention. I’ll probably be back again to read more, thanks for the information!

  9. big Five表示:

    %%

    Here is my homepage; big Five

  10. Parthenia表示:

    %%

    Also visit my homepage: tritype; Parthenia,

  11. open Content表示:

    I wanted to thank you for this great read!! I certainly
    enjoyed every bit of it. I have you book marked to check out new stuff you
    post…

  12. ks quik表示:

    ทางเราจำหน่าย kardinal stick , ks quik ,
    ks kurve ต้องขอบอกได้เลยว่า kardinalstealththailand.com เป็นตัวแทนหลักอย่างเป็นทางการในไทย ที่ใหญ่ที่สุด และเป็นเจ้าเดียวกับ RELX THAILAND สินค้าทุกแบรนด์ ทุกรุ่น เราได้ทำการคัดสรร
    บุหรี่ไฟฟ้า ที่เป็นหนึ่งในนวัตกรรม ช่วยเลิกบุหรี่
    ที่มีประสิทธิดีเยี่ยม และช่วยได้จริง
    มาให้ลูกค้าได้เลือกใช้ โดยสินค้าทุกชิ้นของเรา สั่งตรงจากโรงงาน

  13. ganas69slot表示:

    I pay a quick visit everyday some blogs and information sites to
    read articles or reviews, but this web site provides feature based content.

  14. Allbet表示:

    I’m not sure where you’re getting your information, but great
    topic. I needs to spend some time learning
    much more or understanding more. Thanks for magnificent information I was looking for this information for my mission.

  15. Rodrick表示:

    She is eligible for re-promotion on a non-competitive basis.

    My web blog; Rodrick

  16. Alex Z表示:

    Today we will talk about drdo. Please ask me something

  17. It’s remarkable to visit this web page and reading the views of all mates on the topic of this post, while I am also eager of getting know-how.

  18. socionics表示:

    %%

    Feel free to visit my web page: socionics

  19. Have you ever considered creating an ebook or guest
    authoring on other blogs? I have a blog centered on the same topics you discuss and would love to have you share some
    stories/information. I know my visitors would appreciate your work.
    If you are even remotely interested, feel free to send me an email.

  20. Hey! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any tips?

  21. Jesenia表示:

    %%

    Also visit my web page; togel – Jesenia,

  22. Josephcoume表示:

    can you get viagra over the counter do you need a prescription for female viagra

  23. 군산출장表示:

    What’s up to all, since I am genuinely keen of reading this webpage’s post to be updated regularly.
    It contains pleasant stuff.

  24. make tags表示:

    Τhanks ѕo muсh regardіng gіving everyone an update on thіs ttopic ⲟn your website.

    Ρlease understand tһat if ɑ brand new post appears ⲟr iin tһe event any modifications occur ᴡith tһe
    current publication, Ι wouldd bee considering
    reading ɑ lot mоre аnd learning һow t᧐ makе gokd
    utilization oof tһose approɑches you tɑl abοut.
    Thanks foг your efforts and consideration ߋf othеr individuals by makіng this website ɑvailable.

  25. What a material of un-ambiguity and preserveness of valuable know-how
    regarding unpredicted emotions.

  26. Great internet site! It looks really professional!
    Keep up the helpful work!
    https://www.nowytydzien.pl

  27. Right away I am ready to do my breakfast, when having my breakfast coming over again to read more news.

  28. My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs. But he’s tryiong none the less.
    I’ve been using WordPress on numerous websites for
    about a year and am concerned about switching to another platform.
    I have heard fantastic things about blogengine.net. Is
    there a way I can transfer all my wordpress posts
    into it? Any kind of help would be really appreciated!

發佈留言

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