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,672,261 Responses

  1. Danieljek表示:

    buy amoxicillin 500mg canada http://amoxilcompharm.com/# amoxicillin cephalexin

  2. BrandenSlomy表示:

    fast shipping prednisone: raypharm – cost of prednisone 40 mg

  3. sarang188表示:

    I always emailed this weblog post page to all my associates,
    for the reason that if like to read it then my friends
    will too.

  4. Medicines information for patients. Generic Name.
    can you get cheap trileptal without a prescription
    Everything what you want to know about drugs. Get here.

  5. Kadang link yang keliatannya aman ternyata malah phishing.
    Selalu teliti URL-nya, pastikan protokol HTTPS aktif, dan hindari situs mencurigakan ya!.

  6. BrandenSlomy表示:

    buy 10 mg prednisone: cheap prednisone – prednisone 40 mg price

  7. Aleida表示:

    Many companies involved in the manufacturing of asbestos litigation (Aleida) knew about the dangers
    however they did not inform their employees or their customers.
    These negligent companies should be accountable for their actions.

  8. You really make it seem so easy with your presentation but I find this
    matter to be actually something which I think I would never understand.
    It seems too complicated and extremely broad for
    me. I am looking forward for your next post, I’ll try to get the hang
    of it!

  9. StevenNub表示:

    prednisone tablets india: prednisone – prednisone pharmacy

  10. Dereketerm表示:

    can i buy cheap clomid no prescription: rexpharm – cost of cheap clomid tablets

  11. StevenNub表示:

    medicine prednisone 5mg: prednisone – prednisone for sale without a prescription

  12. wizardshop.cn表示:

    Wizardshop.cc. Welcome to wizard shop cc for hight quality cvv.
    Wizardshop.cc gives you quality cc – Use this
    page to login or register!

  13. Bradleygoath表示:

    generic prednisone pills prednisone 60 mg tablet medicine prednisone 10mg

  14. I like the helpful information you provide in your articles.
    I’ll bookmark your blog and check again here regularly.
    I am quite certain I will learn plenty of new stuff right here!
    Best of luck for the next!

  15. Danieljek表示:

    amoxicillin 500mg https://amoxilcompharm.com/# purchase amoxicillin online without prescription

  16. Bradleygoath表示:

    max pharm Priligy tablets priligy maxpharm

  17. Danieljek表示:

    amoxicillin 500mg pill http://prednisoneraypharm.com/# prednisone 20

  18. Drugs information. Long-Term Effects.
    cost of verapamil for sale
    Some what you want to know about medicines. Get here.

  19. Exo Flux表示:

    Hello, everything is going fine here and ofcourse every
    one is sharing facts, that’s in fact good, keep up writing.

  20. Print Shop表示:

    Good day! This is my first visit to your blog! We are
    a group of volunteers and starting a new initiative in a community in the same
    niche. Your blog provided us useful information to work on. You have done a
    extraordinary job!

  21. Bradleygoath表示:

    prednisone 54899 order Prednisone 200 mg prednisone daily

  22. BrandenSlomy表示:

    prednisone for sale without a prescription: prednisoneraypharm – purchase prednisone canada

  23. Valuable information. Fortunate me I found your web site unintentionally, and I’m shocked why this accident did
    not took place in advance! I bookmarked it.

發佈留言

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