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,228,072 Responses

  1. LarryCoell表示:

    slot oyunlar? puf noktalar? slot tr online en cok kazand?ran slot oyunlar?

  2. and dipterocarps (a family of about 600 species of Old World tropical trees).The one-winged propeller type,高級 ラブドール

  3. ClydePab表示:

    slot tr online: en kazancl? slot oyunlar? – en cok kazand?ran slot oyunlar?

  4. You’ll Never Guess This Window Replacement
    Companies’s Secrets window Replacement

  5. Derekfar表示:

    guvenilir casino siteleri: casino siteleri win – casino siteleri win

  6. Тут можно преобрести купить сейф для охотничьего карабина шкафы оружейные и сейфы

  7. Derekfar表示:

    deneme bonusu veren siteler: deneme bonusu veren siteler betturkey – deneme bonusu veren siteler betturkey

  8. Тут можно преобрести сейфы оружейные купить сейф оружейный купить

  9. Jin69表示:

    Hi there, its nice paragraph on the topic of media print, we all know media is a wonderful source of data.

  10. Тут можно преобрести сейф оружейный купить шкаф для оружия

  11. Kuyaplay – You can Use GCash to play in anytime
    Free Spins 1000+ Free Demo Slot
    Openning Bonus 120% piso back
    or Searching lawinplay on Google or Bing to get free coins

    https://kuyaplayvip.com/

  12. Appreciating the time and effort you put into your website and
    in depth information you present. It’s great to come across a blog
    every once in a while that isn’t the same unwanted rehashed material.
    Great read! I’ve saved your site and I’m including your RSS feeds to my Google account.

  13. Thanks for a marvelous posting! I certainly enjoyed reading
    it, you happen to be a great author. I will remember to bookmark your blog and will eventually come back down the road.
    I want to encourage you to definitely continue your great posts, have
    a nice afternoon!

  14. 10 Things That Everyone Is Misinformed About The Word “Silicone Anal Plug” Anal Sex Butt Plug

  15. LarryCoell表示:

    casino siteleri win Deneme Bonusu Veren Siteler Canl? Casino Siteleri

  16. The Leading Reasons Why People Achieve In The ADHD Assessment UK Private Industry Private adhd Assessment dublin

  17. Google.Co.Cr表示:

    Toto Korea: 10 Things I’d Like To Have Known In The Past 먹튀검증 사이트 (Google.Co.Cr)

  18. Derekfar表示:

    deneme bonusu veren siteler betturkey betturkey.com: deneme bonusu veren yeni siteler – deneme bonusu veren siteler yerliarama.org

  19. 20 Tips To Help You Be More Efficient With Mesothelioma Lawsuit mesothelioma lawsuits (https://historydb.date)

  20. The 10 Most Terrifying Things About White Window Handles white window Handles

  21. Drugs information for patients. Drug Class.
    buy cheap levitra pills
    All what you want to know about drug. Get information here.

  22. What’s The Current Job Market For How To Repair Broken Car Key
    Professionals? how to Repair broken car key

  23. 15 Gifts For The Car Accident Attorneys Lover In Your Life attorney for car accidents near me

  24. You’ll Be Unable To Guess Upvc Double Glazed Windows’s Tricks upvc double glazed Windows

發佈留言

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