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,368,779 Responses

  1. Hurrah! After all I got a weblog from where I can actually get helpful data concerning my study and knowledge.

    Stop by my webpage infant day care

  2. romenico.com表示:

    Please let me know if you’re looking for a article author for your blog.
    You have some really great articles and I think
    I would be a good asset. If you ever want to take some of the load off, I’d love to write some content for your blog in exchange for a link back to mine.

    Please shoot me an e-mail if interested. Regards!

  3. Trey Falcone表示:

    You’ll Never Be Able To Figure Out This Togel4d Login’s Benefits Togel4d login

  4. В магазине сейфов предлагают купить сейф москва новый сейф

  5. Allow me reveal you in my review of NewULife.

    Have a look at my web blog … cbd muscle Balm while Breastfeeding

  6. baby blankets表示:

    We stumbled over here by a different web address and thought I might check things
    out. I like what I see so now i am following you.
    Look forward to looking into your web page yet again.

    Here is my website: baby blankets

  7. Bookmarks表示:

    USP human growth hormone (somatropin).

    Feel free to visit my web page; Bookmarks

  8. Below you find the currently cheapest energy tariffs.

    Check out my web site; Compare electricity plans texas

  9. There are over 60 energy providers in Texas.

    Also visit my website: compare electricity plans australia

  10. Isabella表示:

    USP human growth hormonal agent (somatropin).

    Feel free to surf to my web site :: cbd muscle balm while breastfeeding, Isabella,

  11. The Best Advice You Could Receive About ADHD Diagnosis UK Private Private Adhd assessment guildford

  12. Bookmarks表示:

    USP human development hormone (somatropin).

    Take a look at my page :: Bookmarks

  13. В магазине сейфов предлагают сейфы купить сейф цена

  14. Let me reveal you in my review of NewULife.

    Also visit my blog – euphoria mushroom gummies

  15. RamonRek表示:

    rybelsus cost: rybelsus pill – rybelsus coupon

  16. Bookmarks表示:

    Authentic HGH Advantages (NewULife Hgh Gel Not
    Examined Yet).

    Feel free to surf to my web blog; Bookmarks

  17. Bookmarks表示:

    My partner and I stumbled over here from a different website and thought I might as
    well check things out. I like what I see so now i’m following you.
    Look forward to exploring your web page for a second time.

    Feel free to visit my webpage; Bookmarks

  18. The vapor and flavor high quality is also excellent.

    My website: Canadian plant based meat companies

  19. How An Internet Casino Draws You In And Sucks Your
    Soul 에볼루션 채팅제한

  20. The intelligence is the most effective completely dry natural herb
    vape I have actually attempted.

    Also visit my web site where to buy plant based meals

  21. In this situation, the life insurance business’s money.

    My blog post :: functional mushroom gummies

  22. What’s The Current Job Market For One Cup Coffee Makers Professionals Like? one cup Coffee makers

  23. Jeremypag表示:

    rybelsus cost buy semaglutide online semaglutide cost

  24. I read this paragraph fully about the difference of most recent and previous technologies, it’s awesome article.

    Review my page: Childcare jobs

  25. Bookmarks表示:

    A synthetic USP human development hormone (somatropin).

    Also visit my web blog; Bookmarks

  26. Solar panels and the associated wiring take up space.

    my blog post … compare business electricity prices uk

  27. Bookmarks表示:

    All gas meters offer the same tariffs for usage.

    my web-site; Bookmarks

發佈留言

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