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,623,161 Responses

  1. JesseDex表示:

    пин ап казино зеркало: https://pinupkz.life/

  2. Dwayneseisa表示:

    Tadalafil Easy Buy TadalafilEasyBuy.com cialis without a doctor prescription

  3. vinhomelongan表示:

    Whoa loads of superb knowledge!

  4. JesseDex表示:

    пин ап: https://pinupkz.life/

  5. Dwayneseisa表示:

    TadalafilEasyBuy.com TadalafilEasyBuy.com Tadalafil Easy Buy

  6. Dhaqasho表示:

    A motivating discussion is worth comment. There’s no doubt that that
    you need to publish more on this subject matter, it might not be a taboo
    matter but usually people do not talk about such topics.
    To the next! Best wishes!!

  7. Excellent write-up. I definitely love this site. Stick with it!

  8. pool deck mat表示:

    Remarkable! Its genuinely remarkable article, I have got much clear idea concerning from this post.

  9. Hi mates, nice article and pleasant arguments commented at
    this place, I am really enjoying by these.

  10. RichardHop表示:

    How does it work? A virtual number forwards calls to your primary device, allowing you to communicate as if you were using a regular phone https://nohoho.ru/vremennyj-nomer-telefona-dlya-sms-dlya-chego-mozhet-ponadobitsya.html

  11. Link - Elani表示:

    Digital casinos have revolutionized the betting market, delivering a unique kind of ease and variety that brick-and-mortar gambling houses struggle to rival. In recent years, countless gamblers worldwide have chosen the pleasure of virtual gambling in light of its availability, appealing qualities, and ever-expanding catalogs of games.

    If you’re just starting with the world of internet-based gaming or are looking to delve deeper into trusted platforms, why not join our vibrant social network? It’s a space where gaming aficionados offer tips, helping you to get the most out of your online casino experience. Dive into the connections and see it here now: https://www.spairkorea.co.kr:443/gnuboard/bbs/board.php?bo_table=g_inquire&wr_id=3453617

    One of the most compelling reasons of online gaming options is the sheer diversity of choices provided. Whether you love spinning retro reel games, trying out plot-filled modern slot games, or exercising tactics in traditional table offerings like Roulette, virtual venues boast countless possibilities. Plenty of operators furthermore introduce live casino options, making it possible for you to interact with professional croupiers and other players, all while experiencing the authentic ambiance of a physical gaming house right at home.

    Adding to the extensive catalog, virtual gaming providers shine availability.

  12. It’s really a cool and useful piece of info. I’m glad that you simply shared
    this useful information with us. Please keep us up to date like this.
    Thanks for sharing.

  13. JesseDex表示:

    пин ап вход: https://pinupkz.life/

  14. JesseDex表示:

    пинап казино: https://pinupkz.life/

  15. kontol besar表示:

    I’m gone to inform my little brother, that he should also pay a quick
    visit this weblog on regular basis to get updated from newest information.

  16. sikoflies.com表示:

    Existing ductwork condition and the requirement for brand-new ductwork installment
    effect installation costs.

  17. pornradar.top表示:

    Hi there would you mind letting me know which web host you’re utilizing?
    I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most.
    Can you recommend a good web hosting provider at a reasonable price?
    Many thanks, I appreciate it!

  18. JesseDex表示:

    пин ап: https://pinupkz.life/

  19. Jamesavata表示:

    пинап казино – пин ап казино зеркало

  20. RichardHop表示:

    How does it work? A virtual number forwards calls to your primary device, allowing you to communicate as if you were using a regular phone https://дом-геленджик.рф/2021/09/22/виртуальный-телефонный-номер-как-ра/

  21. Williamuniot表示:

    пин ап зеркало – pinup 2025

  22. I simply could not leave your site prior to suggesting that I actually
    loved the standard info a person provide to your guests?

    Is gonna be back continuously in order to
    check out new posts

    Here is my webpage :: https://Koreaskate.or.kr/game/bbs/board.php?bo_table=free&wr_id=1824949

  23. Williamuniot表示:

    пин ап вход – пинап казино

  24. The portable units usually can not be used to warmth due to the fact that the cool
    air can not be ducted out.

  25. Pavelfus表示:

    Первичный осмотр онколога помогает точную оценку состояния и возможных вариантов лечения. Узнайте больше на сайте — https://vstudent.ru/

發佈回覆給「where can i get baclofen without dr prescription」的留言 取消回覆

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