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,247,230 Responses

  1. BernardMop表示:

    clopidogrel bisulfate 75 mg Clopidogrel Best Prices Cost of Plavix without insurance

  2. Blakesmema表示:

    кухни под заказ — Индивидуальный подход к каждому проекту вашей кухни.

  3. BernardMop表示:

    lisinopril1st cheapest Lisinopril lisinopril1st

  4. CHNDRKIY5表示:

    Thanks for sharing your thoughts on CHNDRKIY5.
    Regards

  5. BrandenSlomy表示:

    amoxicillin 500mg pill: where can i get amoxicillin 500 mg – where to get amoxicillin over the counter

  6. Nice blog here! Also your web site 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

    Here is my webpage – Labelling Machine

  7. It is actually a great and useful piece of information. I am satisfied
    that you simply shared this useful info with us. Please keep us informed like this.

    Thank you for sharing.

  8. BrandenSlomy表示:

    can i purchase cheap clomid: clomid – can i order generic clomid online

  9. 주소월드表示:

    It’s actually very complicated in this full of activity life to
    listen news on TV, therefore I simply use the web for that reason, and take the latest information.

  10. Pretty great post. I just stumbled upon your weblog and wished to say that
    I have really enjoyed surfing around your blog posts. After all I’ll be
    subscribing in your feed and I hope you write once more soon!

  11. BrandenSlomy表示:

    how can i get generic clomid without insurance: cheap clomid – get cheap clomid no prescription

  12. Ccnnews.kr表示:

    Exotic Massage 오피사이트 (Ccnnews.kr)

  13. BrandenSlomy表示:

    can i purchase amoxicillin online: amoxil – amoxicillin script

  14. AMHKTNP9表示:

    Hello I am so excited I found your weblog, I really found you by error,
    while I was researching on Yahoo for something else,
    Nonetheless I am here now and would just like to say cheers for a marvelous post and a all round interesting blog
    (I also love the theme/design), I don’t have time to read
    it all at the moment but I have saved it and also added in your RSS feeds, so
    when I have time I will be back to read a lot more, Please do keep up the excellent job.

  15. BrandenSlomy表示:

    how can i get generic clomid without prescription: clomid – where to buy generic clomid without insurance

  16. Content Spinning fournit des informations précieuses pour vous aider à trouver le bon réformulateur de texte. Vous trouverez des conseils sur comment choisir le meilleur réformulateur de texte.

  17. What i don’t realize is in fact how you are no longer actually much more neatly-preferred than you
    might be right now. You are very intelligent.
    You know therefore considerably in relation to this subject, produced me in my view imagine it from a lot of varied angles.
    Its like men and women aren’t interested unless it is one thing
    to do with Girl gaga! Your own stuffs great. At all times deal with it up!

    Here is my web-site; สมัคร บาคาร่า

  18. yanto mebel表示:

    Hey there! I could have sworn I’ve been to this website before but after reading through some of the post I realized
    it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking
    and checking back frequently!

  19. cuaca778.com表示:

    Hey there! This is my 1st comment here so I just wanted to
    give a quick shout out and say I truly enjoy reading through your posts.

    Can you suggest any other blogs/websites/forums that go over the same topics?
    Thank you!

  20. DarrenUnjuh表示:

    http://lisinopril1st.com/# buy Lisinopril 1st

  21. edelstahlpool表示:

    Good blog you have here.. It’s difficult to
    find high-quality writing like yours nowadays. I truly appreciate individuals like you!
    Take care!!

發佈留言

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