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,233,601 Responses

  1. TylerPhona表示:

    http://cialist.pro/# buy cialis pill

  2. CharlesDog表示:

    https://cialist.pro/# Cheap Cialis

  3. ElmerWes表示:

    Kamagra 100mg buy kamagra online Kamagra tablets

  4. JamesNow表示:

    Buy Vardenafil 20mg online: Levitra 20mg price – Buy Vardenafil online

  5. ElmerWes表示:

    cheapest viagra Order Viagra 50 mg online Viagra online price

  6. 11 Strategies To Refresh Your Black Fridge Freezer best american fridge freezer uk

  7. zapakita01表示:

    Hello it’s me, I am also visiting this site daily, this site
    is really fastidious and the people are actually sharing
    pleasant thoughts.

    Also visit my web site zapakita01

  8. 10 Tell-Tale Signals You Need To Look For A New Upvc Windows
    And Doors Upvc windows repair near Me

  9. You’ll Never Be Able To Figure Out This Treadmill
    Folded Up’s Secrets Treadmill Folded Up

  10. 10 Things You Learned In Kindergarden That’ll
    Help You With Avon Shop With My Rep Login Avon Shop With My Rep Login

  11. Marti表示:

    10 Things We Were Hate About Marc Jacobs Hand Bags marc jacobs bag strap;
    Marti,

  12. JamesNow表示:

    over the counter sildenafil: Buy generic 100mg Viagra online – Order Viagra 50 mg online

  13. 15 Undeniable Reasons To Love Repair Upvc Window upvc window repairs [http://vesti46.ru/user/dugoutdouble2]

  14. I’m really impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it?s rare to see a great blog like this one today..

  15. How To Explain Birth Defect Lawsuit To A Five-Year-Old birth defect Lawsuits

  16. JamesNow表示:

    Purchase Cenforce Online: cenforce.pro – Cenforce 150 mg online

  17. Hi this is kind of of off topic but I was wondering if blogs use
    WYSIWYG editors or if you have to manually code
    with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get
    guidance from someone with experience. Any help would be greatly
    appreciated!

  18. GeorgeSep表示:

    Purchase Cenforce Online: Purchase Cenforce Online – Cenforce 150 mg online

  19. 14 Misconceptions Common To Are CSGO Cases Predetermined huntsman weapon case (minecraftcommand.science)

  20. Lashunda表示:

    You’ll Never Be Able To Figure Out This Double Glazing Repair Leeds’s Tricks double glazing repair leeds (Lashunda)

  21. Window Repair表示:

    You’ll Never Guess This Window Repair Near Me’s Secrets Window Repair

  22. JamesNow表示:

    Cheap Levitra online: Buy generic Levitra online – Buy Vardenafil online

  23. TylerPhona表示:

    https://kamagra.win/# Kamagra Oral Jelly

  24. ラブドール 等身 大 ダッチワイフについてのセックススペシャリストとのミーティング男性が女性の胸に惹かれる3つの理由カップルの性生活からよりインスピレーションを得るには、カジュアルなフックアップとダッチワイフのどちらが良いですか?

  25. ElmerWes表示:

    п»їcialis generic Buy Tadalafil 20mg Buy Tadalafil 5mg

  26. CharlesDog表示:

    https://cenforce.pro/# cheapest cenforce

  27. useful link表示:

    I think this is one of the most important info for me. And i am glad reading your article. But wanna remark on few general things, The site style is ideal, the articles is really great : D. Good job, cheers

  28. JamesNow表示:

    Cialis 20mg price in USA: Generic Cialis without a doctor prescription – Cialis 20mg price

  29. Here’s An Interesting Fact About Espresso Coffee Maker espresso Coffee Machines

  30. ElmerWes表示:

    viagra without prescription Buy Viagra online Order Viagra 50 mg online

發佈留言

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