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,195,733 Responses

  1. Oscar Reys表示:

    How Demo Akun Has Changed My Life The Better Oscar Reys

  2. Medication information leaflet. Drug Class.
    where can i buy cheap zofran price
    Actual trends of medicament. Read now.

  3. Cheap表示:

    This History Behind SEO Consultant In London Can Haunt You Forever!
    Cheap

  4. A Look At The Good And Bad About Double Glazing Installers Near
    Me double glazed wooden windows

  5. arlennizo.top表示:

    The Best Advice You Can Ever Receive On Boot Scooters
    arlennizo.top

  6. Medicament information for patients. Effects of Drug Abuse.
    can you get cheap ramipril without a prescription
    Best about medicines. Get now.

  7. Archer表示:

    Here is my site … top 10 poker websites (Archer)

  8. You Can Become A Credit History Personal Loan For $5,000 중기청 대출

  9. 7YhHEK表示:

    Hello, of course this article is really nice and I have learned lot of things from it regarding blogging.
    thanks.

  10. Hi, I want to subscribe for this website to take most recent updates, therefore where
    can i do it please help.

  11. How Much More Your Balance Transfers Credit Card 신혼부부 대출

  12. When someone writes an article he/she retains the thought of a user in his/her brain that how a user
    can know it. Thus that’s why this piece of writing is
    outstdanding. Thanks!

  13. Alfred表示:

    5 Attorney For Asbestos Projects For Any Budget Alfred

  14. game gacor表示:

    My brother suggested I might like this web site.

    He was entirely right. This post truly made my day.
    You cann’t imagine simply how much time I had spent for this info!
    Thanks!

  15. What’s The Current Job Market For Upvc Repairs Near Me Professionals?
    Upvc repairs near me (talkstarsi.com)

  16. Great article.

    Also visit my site Does PhenQ Work

  17. 9 Things Your Parents Teach You About ADHD Uk Medication Adhd uk medication

  18. Medicines information for patients. Short-Term Effects.
    can i get generic zocor prices
    Everything news about medicines. Get now.

  19. casino表示:

    lesbian porn

  20. Drugs information. Short-Term Effects.
    how to get cheap zithromax without dr prescription
    Actual trends of medicines. Read here.

  21. Pills information leaflet. Cautions.
    where to get generic indomethacin no prescription
    Actual what you want to know about medication. Get information here.

  22. warp888表示:

    Because the admin of this web site is working,
    no hesitation very rapidly it will be renowned, due to its feature contents.

  23. Have you ever thought about writing an ebook or guest authoring on other websites?
    I have a blog centered on the same subjects you discuss
    and would really like to have you share some stories/information. I know my
    viewers would enjoy your work. If you’re even remotely interested,
    feel free to shoot me an e-mail.

  24. near me表示:

    Nine Things That Your Parent Teach You About Double
    Glazing Repair Near Me near me

  25. Hey there! Would you mind if I share your blog with my myspace group?
    There’s a lot of people that I think would really appreciate your
    content. Please let me know. Thanks

  26. Sage Advice About Malpractice Lawyer From A Five-Year-Old Malpractice Attorney

  27. Heya! I’m at work browsing your blog from my new apple iphone!
    Just wanted to say I love reading your blog and look forward to
    all your posts! Keep up the outstanding work!

發佈留言

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