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

  1. Charlesbot表示:

    https://fastpillseasy.com/# where to get ed pills

  2. загранпаспорт срочно москва [url=https://www.astana.forum24.ru/?1-0-0-00000564-000-0-0-1735286925]загранпаспорт срочно москва[/url] .

  3. вызов нарколога на дом частная скорая помощь вызов нарколога на дом частная скорая помощь .

  4. 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트

  5. Thanks for the great explanation. More info at services offered by Sculpted MD

  6. hiop表示:

    Culture Shock And International Business – Tips For Women hiop

  7. Hulda Blair表示:

    The tips shared here are invaluable! For those needing repair services after water damage, try contacting water damage restoration

  8. Buster表示:

    10 In Order To Look When Ever Choosing A Hen Party Venue 광주유흥 (Buster)

  9. I think finding the perfect salon is essential for any individual that loves their hair! For pointers and recommendations, I very recommend going to Brazilian Blowout SF Keratin Treatment

  10. RogerFaf表示:

    https://fastpillseasy.com/# how to get ed pills

  11. What i do not realize is in truth how you’re no longer really much more neatly-favored than you might be now.
    You are very intelligent. You recognize therefore significantly in relation to this subject,
    produced me in my view imagine it from so many numerous angles.
    Its like men and women don’t seem to be involved unless it’s something to do with Girl gaga!

    Your individual stuffs nice. All the time care for it up!

  12. Edwin Lewis表示:

    Can’t wait to spend more time by the pool now that my screens are fixed thanks to All Screening of SWFL! Pool Cage Screen Repair

  13. RogerFaf表示:

    http://fastpillseasy.com/# cheapest ed meds

  14. неотложная наркологическая помощь в москве http://www.vishivayu.ukrbb.net/viewtopic.php?f=12&t=13503/ .

  15. This was highly helpful. For more, visit juegos de casino gratis para jugar

  16. Just had my aircon fastened, and I’m so relieved! If someone’s searching out legitimate amenities in Canoga Park, you may still visit ac repair service for extensive recommendations

  17. Barrybib表示:

    viagra without prescription: FastPillsForMen.com – buy Viagra over the counter

  18. I just couldn’t depart your website prior to suggesting that I actually enjoyed the usual info a person supply in your guests?

    Is going to be back incessantly to inspect new posts

  19. неотложная наркологическая помощь в москве неотложная наркологическая помощь в москве .

  20. срочное изготовление загранпаспорта срочное изготовление загранпаспорта .

  21. Can’t thank my chiropractor enough for helping me bounce back after my accident! Anyone else had a similar experience? If so Chiropractor Kent, WA

  22. RogerFaf表示:

    https://fastpillseasy.com/# buy erectile dysfunction treatment

  23. RogerFaf表示:

    https://maxpillsformen.com/# Buy Cialis online

  24. Lenora Owen表示:

    It’s amazing how chiropractic care can help with pain relief after an accident! I discovered some great resources at Lakewood WA Chiropractor that others might find useful too

  25. вызов нарколога на дом частная скорая помощь вызов нарколога на дом частная скорая помощь .

  26. RogerFaf表示:

    https://fastpillsformen.com/# Generic Viagra online

發佈留言

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