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,146,801 Responses

  1. Assessment Mental Health Tips To Relax Your Daily Lifethe One Assessment Mental Health
    Trick That Every Person Should Know Assessment Mental Health

  2. Hi from explainer video company india, just became aware of your blog through Google Explainer video Company india, and found that it is really informative. I am gonna watch out for brussels. I?ll appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers! https://www.google.com/maps/d/u/1/viewer?hl=en&hl=en&mid=1cckKkHw2iSzG_ucdHmfXyXdKxu64d5k

  3. key Fob volvo表示:

    9 . What Your Parents Teach You About Key Fob Volvo key Fob volvo

  4. pasien88.com表示:

    Pretty portion of content. I simply stumbled upon your blog and in accession capital to claim that I get in fact loved
    account your blog posts. Any way I’ll be subscribing to your feeds or even I success you get admission to consistently fast.

  5. How To Explain Walking Pad For Desk To Your Grandparents Walk pad For desk

  6. GeorgeSep表示:

    generic sildenafil: Cheapest place to buy Viagra – Viagra generic over the counter

  7. CharlesDog表示:

    https://levitrav.store/# Buy Vardenafil 20mg

  8. Hello Dear, are you truly visiting this website on a regular basis, if so then you will
    absolutely take nice experience.

  9. ElmerWes表示:

    buy Levitra over the counter Buy Vardenafil 20mg Levitra 10 mg buy online

  10. Excellent post. I was checking continuously this explainer video blog and I am impressed! Very useful info particularly the last part 🙂 I care for such info a lot. I was looking for this certain info for a very long time. Thank you and best of luck. https://classified4u.Biz/0/posts/3-services/27-other/1938408-explainer-video-company-india.Html

  11. JamesNow表示:

    sildenafil online: Buy Viagra online – Order Viagra 50 mg online

  12. Upvc Repair表示:

    The Best Advice You Could Ever Receive On Repairs To Upvc Windows Upvc Repair

  13. CharlesDog表示:

    https://viagras.online/# over the counter sildenafil

  14. ElmerWes表示:

    buy cialis pill cialist.pro Cialis 20mg price

  15. How To Become A Prosperous Double Glazing Repair Leeds Entrepreneur Even If
    You’re Not Business-Savvy double Glazed window Repairs

  16. 10 Things We All Are Hating About Shopping Online 10 Pack light Bulbs

  17. Hi from explainer video company india, just became alert to your blog through Google Explainer video Company india, and found that it is truly informative. I?m gonna watch out for brussels. I?ll appreciate if you continue this in future. Many people will be benefited from your writing. Cheers! https://directory-url.com/listings12710132/explainer-video-company-india

  18. TylerPhona表示:

    https://levitrav.store/# Levitra online USA fast

  19. Normand表示:

    7 Things About Online Shopping Sites For Clothes You’ll Kick Yourself For Not
    Knowing us online shopping sites for clothes; Normand,

  20. Verlene表示:

    5 Things That Everyone Is Misinformed About In Regards To Erb’s Palsy Law erb’s palsy law firms – Verlene

  21. CharlesDog表示:

    https://levitrav.store/# buy Levitra over the counter

  22. 15 Weird Hobbies That Will Make You Better At Marc Jacobs Handbag marc jacobs bag shoulder

  23. ElmerWes表示:

    Kamagra 100mg buy kamagra online sildenafil oral jelly 100mg kamagra

  24. Excellent post. I was checking continuously this explainer video blog and I am impressed! Very useful info specifically the last part 🙂 I care for such information much. I was seeking this particular information for a very long time. Thank you and best of luck. https://www.cleansway.com/business/explainer-video-company-india

  25. ElmerWes表示:

    Cialis without a doctor prescription cialist.pro cialis for sale

  26. Rudy表示:

    20 Things You Should Be Educated About Truck Accident Attorneys truck accident lawyer in atlanta (Rudy)

  27. JamesNow表示:

    Cheap Levitra online: levitrav.store – Cheap Levitra online

  28. Does your blog have a contact page? I’m having problems locating
    it but, I’d like to send you an email. I’ve got some
    recommendations for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it expand over time.

  29. ElmerWes表示:

    Cenforce 100mg tablets for sale buy cenforce cenforce.pro

發佈留言

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