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,480,080 Responses

  1. Beulah Wise表示:

    Great insights into roofing jobs! Can anyone recommend a reliable roofers near me

  2. injury lawyer表示:

    Five Toledo Birth Injury Attorneys Lessons From The Professionals injury lawyer

  3. Lilly Patrick表示:

    Love seeing our community support local businesses like hardworking #roofing contractors#! roof replacement St. Louis

  4. Mina Ward表示:

    Jiu-jitsu has entirely transformed my health and fitness trip! If you’re curious concerning exactly how to get going or enhance your video game, look into the useful sources at no gi jiu jitsu

  5. ebony mom xxx表示:

    I am truly thankful to the holder of this website
    who has shared this enormous article at here.

  6. Pearl Ellis表示:

    This is quite enlightening. Check out عمل پی ار کی for more

  7. Diplomi_ropt表示:

    купить диплом института в кемерово prema-diploms.ru .

  8. Barrybib表示:

    Viagra online price: buy viagra online – Cheap generic Viagra online

  9. viagra表示:

    Appreciating the time and energy you put into your site and detailed information you provide.
    It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed material.
    Wonderful read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

  10. A Quick Guide To Being A Guarantor To Obtain Home Loan 다바오 머니 상

  11. 10 Locations Where You Can Find Mobile Automobile Locksmith mobile automotive locksmith

  12. Lloyd Mullins表示:

    Appreciate the helpful advice. For more, visit pool installation in paterson

  13. загранпаспорт стоимость [url=https://guryevsk.forum24.ru/?1-4-0-00000570-000-0-0-1735286442]https://guryevsk.forum24.ru/?1-4-0-00000570-000-0-0-1735286442[/url] .

  14. where can i buy generic motilium price can i get cheap motilium without prescription can i get generic motilium for sale
    can i order motilium pill how can i get motilium tablets can i get generic motilium without insurance
    where to get generic motilium pill
    where buy cheap motilium pill buy generic motilium no prescription buy cheap motilium without insurance
    get generic motilium tablets buying generic motilium pills where buy cheap motilium no prescription

  15. magnificent publish, very informative. I wonder why
    the opposite experts of this sector do not realize
    this. You must proceed your writing. I am sure,
    you have a huge readers’ base already!

  16. BrettTIT表示:

    Order Viagra 50 mg online: buy viagra online – buy Viagra online

  17. 7 Great Spring Vacation Travel Destinations 하이오피사이트

  18. Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Liked it!

  19. It’s amazing how chiropractic care can help with pain relief after an accident! I discovered some great resources at Lakewood WA Chiropractor that others might find useful too

  20. WD808表示:

    Hmm it appears like your site ate my first comment (it was extremely
    long) so I guess I’ll just sum it up what I wrote and say,
    I’m thoroughly enjoying your blog. I as well am an aspiring blog blogger but I’m still new to the whole thing.
    Do you have any tips for beginner blog writers? I’d definitely appreciate
    it.

  21. 10 Things Everyone Has To Say About Search Engine Optimisation Agency Search Engine Optimisation Agency seo agency london (https://ai-db.science/)

  22. GOLDPAY表示:

    How To Obtain New Business GOLDPAY

  23. Donaldabula表示:

    cheapest ed pills cheap cialis п»їed pills online

  24. Chase Gilbert表示:

    This post has inspired me to prioritize my spine health! Anyone know a good chiropractor in Kent Auto Injury chiropractor

  25. Diplomi_bjpt表示:

    купить диплом кемерово купить диплом кемерово .

  26. Diplomi_htpi表示:

    ответственность за купленный диплом 4russkiy365-diplomy.ru .

發佈留言

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