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,246,396 Responses

  1. Reforestation for plantation of Agriculture Land

  2. Hello, i feel that i noticed you visited my website so i got here to return the desire?.I
    am trying to to find things to enhance my website!I guess its adequate to use a few of your concepts!!

  3. This piece of writing is in fact a pleasant one it helps new web users, who are wishing in favor of blogging.

  4. Markdob表示:

    Всем привет! Может кто знает, где почитатьразные блоги о недвижимости? Сейчас читаю – https://oookuper.ru

  5. BOKEP INDO表示:

    Hi exceptional website! Does running a blog such as this take a lot
    of work? I’ve virtually no understanding of computer programming but I was hoping
    to start my own blog soon. Anyhow, should you have any recommendations or tips for new blog owners
    please share. I know this is off topic but I just wanted to ask.
    Kudos!

  6. you are really a excellent webmaster. The web site loading speed is amazing.
    It kind of feels that you’re doing any unique trick. In addition, The contents are masterwork.
    you’ve performed a excellent job on this topic!

  7. When you are looking for a poster of Kanye West, you’ll find a huge variety
    of artistic prints. Featuring his top releases like 808s & Heartbreak,
    and even to modern styles, you’re bound to find
    something for any Kanye fan. Kanye West wall art will enhance
    your interior.

  8. алкоголизм вывод из запоя краснодар алкоголизм вывод из запоя краснодар .

  9. Thank you for sharing your info. I really appreciate your efforts and
    I am waiting for your further write ups thank you
    once again.

  10. Markdob表示:

    Приветствую. Может кто знает, где почитатьразные блоги о недвижимости? Сейчас читаю – https://oookuper.ru

  11. AlbertDuM表示:

    Lisinopril 1st: Lisinopril 1st – buy Lisinopril 1st

  12. whoah this blog is excellent i like reading your posts.
    Stay up the good work! You understand, lots of individuals are looking round for this
    info, you could aid them greatly.

  13. Ahaa, its fastidious conversation concerning
    this post at this place at this weblog, I have read all that,
    so at this time me also commenting at this place.

  14. BrandenSlomy表示:

    where can i buy amoxicillin online: Amoxicillin Com Pharm – buy amoxicillin online uk

  15. Jane Richards表示:

    Sự kích thích từ từng vòng quay may mắn khiến trái tim mình loạn nhịp; chắc chắn rằng mọi thứ sẽ trở nên hoàn hảo hơn nếu bạn chọn đúng nhà cái như thế này! B52Club

  16. variksss表示:

    Wonderful goods from you, man. I’ve understand your stuff previous
    to and you are just too great. I actually like what you
    have acquired here, really like what you’re saying and the way in which you say it.
    You make it entertaining and you still take care of to keep it smart.
    I can not wait to read much more from you. This is really a tremendous web site.

  17. Hi there mates, how is all, and what you desire to say regarding this post, in my
    view its in fact amazing in support of me.

  18. Stella表示:

    When you’re on the hunt for Kanye West wall art, you’re sure to find tons of stylish
    pieces. Whether it’s inspired by his classic albums like Yeezus, and even to latest collections, there’s definitely something that matches any art collector.
    Kanye West wall art will elevate your office setup.

  19. Henry Torres表示:

    I recently had my screen replaced, and I was amazed at how quickly the repair was done! If you’re looking for reliable phone repair services, check out computer shop for great tips and support

  20. Thật khó khăn để có thể rời mắt khỏi màn hình máy tính mỗi khi bắt đầu trận đấu kịch tính trên nền tảng online này đó mọi người ơi! B52Club

  21. Higly Demand of Durian Land

  22. Узнай все о искривление перегородки носаискривление перегородки носа операция

  23. 10 Meetups About Male Mastubators You Should Attend best Masturbator (4k0bv8c1zgcwnr1o.kr)

  24. AlbertDuM表示:

    Clopidogrel 75 MG price: buy plavix online – cheap plavix antiplatelet drug

發佈留言

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