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,198,610 Responses

  1. Paragraph writing is also a excitement, if you be acquainted with then you can write otherwise
    it is complicated to write.

  2. 10 Graphics Inspirational About Kia Sportage Key Fob
    Kia Smart Key Programming

  3. The 10 Scariest Things About Rush Sugar Demo rush Sugar Demo

  4. How Double Glazed Replacement Glass Near Me Was The Most Talked About Trend Of 2023 Double glazing near me

  5. meslot889表示:

    Nice post. I was checking continuously this blog and I am impressed!
    Very useful info specifically the last part 🙂 I care for such information much.
    I was seeking this certain information for a very long time.
    Thank you and best of luck.

  6. 10 Things We Are Hating About Double Glazing Near Me double glazing Company near me

  7. Medicament information sheet. Long-Term Effects.
    where buy generic azathioprine
    Some what you want to know about medicine. Read information now.

  8. Marcel表示:

    Nine Things That Your Parent Teach You About Double Glazed Window Suppliers Near Me double
    glazed window suppliers near me (Marcel)

  9. What’s The Job Market For Fela Railroad Accident Lawyer Professionals Like?
    Fela railroad accident lawyer

  10. Meds information sheet. Long-Term Effects.
    buying cheap nortriptyline tablets
    All what you want to know about medicines. Read now.

  11. Helpful info. Fortunate me I discovered your site by chance,
    and I am shocked why this coincidence did not took place in advance!

    I bookmarked it.

  12. How To Save Money On Psychiatry Assessment Uk Assessment In psychiatry

  13. Drugs information sheet. What side effects can this medication cause?
    buying generic sumatriptan for sale
    Everything what you want to know about drugs. Get information here.

  14. Приветствую. Подскажите, где найти полезные блоги о недвижимости? Пока нашел https://vortex-los.ru

  15. GSA SER List表示:

    I was recommended this website through my cousin. I am now not sure
    whether or not this post is written by means of him as nobody else know such
    distinct about my difficulty. You’re amazing! Thanks!

  16. Medicine information for patients. Long-Term Effects.
    cheap xenical without insurance
    Some about drug. Read information here.

  17. Attorney表示:

    4 Dirty Little Secrets About The Veterans Disability Litigation Industry Attorney

  18. You’ll Never Guess This Double Glaze Repair Near Me’s Secrets double glaze repair near me (http://hotelrocio.kr/bbs/board.php?bo_table=free&wr_id=69493)

  19. Guide To Double Glazed Window Near Me: The Intermediate Guide The
    Steps To Double Glazed Window Near Me double glazed window near me

  20. 023456789表示:

    What Experts From The Field Want You To Know? 023456789

  21. sex表示:

    Hi there, I desire to subscribe for this webpage to obtain latest updates, therefore where can i do it please help out.

  22. Приветствую. Может кто знает, где почитать разные блоги о недвижимости? Пока нашел https://vortex-los.ru

  23. Seven Explanations On Why Veterans Disability Case Is
    Important veterans disability lawyer

  24. arlennizo表示:

    You Can Explain Collapsible Scooter To Your Mom arlennizo

  25. Medicine information for patients. Generic Name.
    can i purchase atarax for sale
    All trends of medication. Read here.

  26. Medicine information for patients. Long-Term Effects.
    how can i get cheap dilantin without rx
    Best information about pills. Get information here.

  27. Medication information leaflet. What side effects can this medication cause?
    how to get cheap lisinopril pills
    Some trends of drugs. Read information now.

  28. Stephanie表示:

    Hey There. I found your blog using msn. This is an extremely well
    written article. I’ll be sure to bookmark it and come back to read more of
    your useful info. Thanks for the post. I will certainly return.

發佈留言

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