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,248,737 Responses

  1. BrandenSlomy表示:

    dapoxetine price: dapoxetine online – buy dapoxetine online

  2. Curtisduh表示:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  3. Markdob表示:

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

  4. Nikefut表示:

    darknet market darknet markets 2024 https://darknetmarketstore.com/ – dark web market

  5. Hi there, after reading this remarkable piece of writing i am as well happy to share my familiarity here
    with colleagues.

  6. I don’t even know how I ended up here, but I thought this post was good.

    I do not know who you are but definitely you are going to a famous blogger if you are not already
    😉 Cheers!

  7. Christoper表示:

    Five Killer Quora Answers To Programing Keys programing key
    [Christoper]

  8. Avocat CNESST表示:

    I am sure this post has touched all the internet people, its really really pleasant paragraph on building up new webpage.

  9. BrandenSlomy表示:

    clomid tablet: cost of clomid without insurance – generic clomid online

  10. Someone necessarily lend a hand to make seriously articles
    I might state. This is the first time I frequented your web page and thus far?
    I amazed with the research you made to make this particular put
    up extraordinary. Magnificent task!

  11. StevenNub表示:

    priligy max pharm: priligy – priligy maxpharm

  12. Nikefut表示:

    deep web search drug markets onion https://darknetmarketstore.com/ – dark web links

  13. BrandenSlomy表示:

    where to get generic clomid: where can i get generic clomid without rx – can i get clomid without dr prescription

  14. Pretty! This was an extremely wonderful article. Thanks for providing this info.

  15. This is my first time pay a quick visit at here and i am really pleassant to read all at one place.

  16. led neon sign表示:

    Thanks for any other informative web site. The place else could I get that kind of info written in such
    a perfect means? I’ve a project that I am just now working on, and I have been on the look out for such info.

  17. Hmm is anyone else encountering problems with the images
    on this blog loading? I’m trying to figure out if its
    a problem on my end or if it’s the blog. Any feedback would be
    greatly appreciated.

  18. BrandenSlomy表示:

    where to buy generic clomid online: generic clomid – get generic clomid without dr prescription

  19. The Fitbit Charge is a good fitness tracker.. however feels somewhat
    confused with the Fitbijt family.

    Here is my homepage :: sgสล็อต

  20. StevenNub表示:

    order clomid tablets: can i buy generic clomid without rx – get cheap clomid without dr prescription

  21. Great blog right here! Additionally your web site quite
    a bit up fast! What web host are you the usage of?
    Can I get your affiliate hyperlink to your host? I wish my web site loaded up as quickly as yours lol

  22. Hello to all, because I am genuinely keen of reading this blog’s post to be updated regularly.
    It consists of pleasant information.

  23. dewibola表示:

    Fantastic items from you, man. I’ve bear in mind your
    stuff prior to and you’re just extremely wonderful.

    I really like what you’ve bought right here, really like what
    you’re saying and the best way in which you assert it. You make it enjoyable and you still take care of to keep
    it sensible. I can not wait to learn much more from you.
    That is really a tremendous site.

  24. What Is Driving License C? History Of Driving License C
    prawo jazdy na automat

  25. Dereketerm表示:

    amoxicillin 775 mg: buy amoxicillin online without prescription – amoxicillin 500mg capsules price

  26. StevenNub表示:

    where can i buy clomid without rx: rex pharm – clomid buy

  27. website bokep表示:

    Very good article. I definitely love this site.
    Stick with it!

發佈留言

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