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,624,738 Responses

  1. BrandonSaisy表示:

    how to cure ed naturally: cheap pharmacy – natural treatments for ed

  2. I know this if off topic but I’m looking into starting my
    own blog and was curious what all is required to get set
    up? I’m assuming having a blog like yours would cost
    a pretty penny? I’m not very web smart so I’m not 100% sure.
    Any suggestions or advice would be greatly appreciated. Thank you

  3. BrandonSaisy表示:

    causes for ed: cheap medication – muse for ed

  4. Equinunk表示:

    I think the admin of this web page is truly working hard in favor of his
    web page, because here every stuff is quality based data.

  5. Lazrzbg表示:

    Как приобрести диплом техникума с минимальными рисками
    freshhomes.ru/?post_type=topic&p=10159

  6. DevonPhack表示:

    http://pharm24.pro/# ed pills that work quickly
    ways to treat erectile dysfunction

  7. Hello to all, how is everything, I think every one is getting more from this web site,
    and your views are good in support of new users.

  8. 11 Ways To Completely Revamp Your Asbestosis Asbestos Mesothelioma Attorney Mesothelioma
    Lawsuit (Fox-Kirkland-2.Technetbloggers.De)

  9. AGRO TRADE表示:

    Hi! Would you mind if I share your blog with my zynga group?
    There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Cheers

  10. Williamagric表示:

    male erectile dysfunction https://pharm24.pro/# natural ed pills

  11. I am truly grateful to the holder of this website who has shared this great post
    at here.

  12. You’ll Never Be Able To Figure Out This Replacement Double Glazing Window Handles’s
    Benefits double glazing window Handle

  13. The 10 Most Terrifying Things About Accident Attorneys Accident Attorneys

  14. Every weekend i used to pay a quick visit this web page, for the reason that i want enjoyment, for the
    reason that this this web site conations actually good funny material too.

  15. 7 Secrets About Bluetooth Sex Machine That Nobody Will
    Share With You sex Toys machines

  16. Aaronrem表示:

    Ищете профессиональных грузчиков и транспорт для переезда? Рекомендую обратиться в эту компанию здесь https://linktr.ee/gruzprof

  17. There are various tools and websites that claim to allow users to view private Instagram profiles, but it’s important to
    way in these bearing in mind 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 habit to view
    a private profile is to send a follow request directly to the user.
    Always prioritize privacy and honoring in your online interactions.

    Here is my homepage … private instagram viewer free

  18. Williamagric表示:

    ed doctors https://mexicanpharm24.cheap/# reputable mexican pharmacies online

  19. BrandonSaisy表示:

    pharmacy website india: Pharmacies in India that ship to USA – indian pharmacy paypal

  20. This is my first time go to see at here and i am genuinely happy to read everthing at single
    place.

  21. 7 Secrets About Second Hand Couches For Sale That Nobody Will Share With You leather couches for sale Near me

  22. bokep xnxx表示:

    Great post. I used to be checking constantly this blog and I am
    impressed! Extremely helpful information specially the last phase :
    ) I care for such info much. I was looking
    for this certain info for a long time. Thank you
    and good luck.

  23. Woah! I’m really digging the template/theme of this website.

    It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between user friendliness and appearance.
    I must say you have done a amazing job with this. Also,
    the blog loads super fast for me on Internet explorer. Superb Blog!

  24. Norman表示:

    Guide To Treadmill Best: The Intermediate Guide The Steps To Treadmill Best treadmill best (Norman)

  25. The 10 Scariest Things About Audi Car Keys Replacement Audi Car Keys Replacement

發佈留言

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