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,245,891 Responses

  1. Nikefut表示:

    deep web drug url darknet market https://darknetmarketstore.com/ – darknet sites

  2. StevenNub表示:

    where can i get prednisone over the counter: prednisone ray pharm – how can i order prednisone

  3. Dereketerm表示:

    cost of prednisone 40 mg: prednisone – online order prednisone 10mg

  4. Louissuext表示:

    Pill effects explained. Contraindications explained here.
    buy finasteride no prescription
    Short-term impacts described. Drug impacts explained.

  5. There are various tools and websites that affirmation to
    allow users to view private Instagram profiles,
    but it’s important to retrieve these past caution. Many
    of these tools can be unreliable, may require personal information, or could violate
    Instagram’s terms of service. Additionally, using such tools
    can compromise your own security or guide to scams.
    The safest and most ethical way to view a private instagram viewer profile is to
    send a follow demand directly to the user. Always prioritize privacy and adulation in your online interactions.

  6. BrandenSlomy表示:

    buy dapoxetine online: Priligy tablets – cheap priligy

  7. Fabulous, what a website it is! This website presents
    valuable data to us, keep it up.

  8. BrandenSlomy表示:

    amoxicillin 500mg over the counter: com pharm – buy amoxicillin 500mg

  9. 1xbet表示:

    {

    Feel free to visit my web site 1xbet

  10. BrandenSlomy表示:

    priligy maxpharm: cheap priligy – Priligy tablets

  11. StevenNub表示:

    priligy: dapoxetine price – priligy maxpharm

  12. Hi there i am kavin, its my first time to commenting anyplace,
    when i read this article i thought i could also create comment due
    to this brilliant post.

  13. I simply could not depart your website prior to suggesting that I actually loved the usual info a person supply on your guests?
    Is gonna be back steadily to check out new posts

  14. Now I am going to do my breakfast, afterward having my breakfast coming
    over again to read additional news.

  15. StevenNub表示:

    amoxicillin order online: buy amoxil online – amoxicillin 500 mg tablet price

  16. BrandenSlomy表示:

    prednisone 10mg tabs: prednisone – prednisone uk over the counter

  17. Cool blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would really make my blog jump
    out. Please let me know where you got your theme.
    Bless you

  18. Hello friends, good article and nice arguments commented at this place, I am really enjoying by these.

  19. Nikefut表示:

    dark web search engine dark websites https://darknetmarketstore.com/ – darknet links

  20. Do you mind if I quote a couple of your posts as long as
    I provide credit and sources back to your site?
    My blog is in the exact same niche as yours and my users would genuinely benefit from a lot of the information you provide here.

    Please let me know if this okay with you.
    Appreciate it!

  21. StevenNub表示:

    amoxicillin buy no prescription: amoxil com pharm – amoxicillin 1000 mg capsule

  22. casinoapp表示:

    This is really interesting, You are a very skilled blogger.
    I’ve joined your feed and look forward to seeking more
    of your wonderful post. Also, I have shared your website in my social networks!

  23. There are various tools and websites that claim to permit
    users to view private instagram viewer Instagram profiles, but it’s important to log on these
    subsequently caution. Many of these tools can be unreliable, may require personal information, or could violate Instagram’s terms of service.
    Additionally, using such tools can compromise your own security or lead
    to scams. The safest and most ethical artifice to view a private profile is
    to send a follow demand directly to the user. Always prioritize privacy and esteem in your online
    interactions.

  24. StevenNub表示:

    how to purchase prednisone online: cheap prednisone – prednisone pharmacy

  25. Hi there, after reading this amazing article i am
    as well delighted to share my know-how here with colleagues.

發佈留言

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