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,233,347 Responses

  1. HassanbuifS表示:

    https://casinositeleri.win/# casino siteleri win
    deneme bonusu veren yeni siteler

  2. HassanbuifS表示:

    https://ultrabet-tr.online/# ultrabet
    deneme bonusu veren siteler

  3. LarryCoell表示:

    ultrabet ultrabet ultrabet tr online

  4. ovalpool表示:

    Hello There. I found your blog using msn. This is an extremely well written article.
    I’ll make sure to bookmark it and come back to
    read more of your useful information. Thanks for the post.
    I’ll definitely comeback.

  5. 10 Facts About 3 Wheeled Pushchair That Will Instantly Put You In A Good Mood 3 wheeled Strollers

  6. CarrollUnton表示:

    deneme bonusu veren yeni siteler http://ultrabet-tr.online/# ultrabet bonus

  7. HassanbuifS表示:

    https://matadorbet.bid/# matadorbet bid
    deneme bonusu veren siteler yeni

  8. Medicament information leaflet. Generic Name.
    how to buy cheap cephalexin
    Actual information about medicines. Get information here.

  9. ラブドール えろYou might think you’re likely to eat more when you’re really hungry or if you’re digging in to one of your favorite dishes.But that’s not always the case.

  10. LarryCoell表示:

    deneme bonusu veren siteler mycbet.com deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler

  11. Derekfar表示:

    slot oyunlar? puf noktalar?: slot oyunlar? – slot tr online

  12. Why You Should Focus On Making Improvements Bentley Valet Key New bentley Key

  13. Sazrgal表示:

    Как получить диплом техникума с упрощенным обучением в Москве официально

    filcherp.tforums.org/viewtopic.php?f=2&t=31222

  14. WOW just what I was searching for. Came here by searching for glucotrust benefits

  15. online表示:

    Great article, exactly what I wanted to find.

  16. Treftuo表示:

    Приобретение диплома ПТУ с сокращенной программой обучения в Москве
    rakisochi.ru/users/20

  17. Godfather789表示:

    Godfather789ยเว็บตรงของคนไทย เจ้าพ่อมาเฟียเว็บใหญ่ไม่มีโกง

  18. What’s The Job Market For Automatic Folding Travel Mobility Scooter Professionals Like?

    automatic Folding Travel mobility scooter

  19. Derekfar表示:

    ultrabet tr online: ultrabet giris – ultrabet yeni giris 1125

  20. porn jav表示:

    I like what you guys are usually up too. This type of clever
    work and reporting! Keep up the fantastic works
    guys I’ve included you guys to my blogroll.

  21. Derekfar表示:

    deneme bonusu veren yeni siteler: deneme bonusu veren siteler – deneme bonusu veren siteler denemebonusu2026.com

  22. cephalexinpve表示:

    Drugs information for patients. What side effects can this medication cause?
    cephalexinpve
    Actual about medicine. Get information now.

  23. The 12 Most Unpleasant Types Of Bean To Cup Machine Accounts You
    Follow On Twitter Which Bean To Cup

  24. cybersecurity表示:

    What a data of un-ambiguity and preserveness of valuable know-how about unpredicted feelings.

  25. Trefgdb表示:

    Узнайте стоимость диплома высшего и среднего образования и процесс получения
    files.4adm.ru/viewtopic.php?f=2&t=803

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

發佈留言

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