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,235,528 Responses

  1. Drugs information. What side effects can this medication cause?
    azithromycin making me feel sick
    Actual trends of pills. Read information now.

  2. I constantly spent my half an hour to read this blog’s content every day along with a cup of
    coffee.

  3. Тут можно преобрести магазин сейфы для оружия сейф оружейный

  4. Tory表示:

    The Unspoken Secrets Of Private ADHD Assessment Near Me private adhd assessment dundee (Tory)

  5. CarrollUnton表示:

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

  6. It’s remarkable in support of me to have a
    website, which is valuable in favor of my experience.
    thanks admin

  7. LarryCoell表示:

    ultrabet tr online ultrabet giris ultrabet guncel

  8. Where Are You Going To Find Folding Electric Wheelchairs For Sale
    Be One Year From Today? Lightest electric Folding wheelchair

  9. The 10 Most Scariest Things About Senior Mobility Scooters Premium mobility
    Scooters (mysocialguides.Com)

  10. ClydePab表示:

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

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

  12. I don’t even understand how I stopped up here, however I assumed
    this publish used to be great. I do not know who you’re however definitely you’re going
    to a well-known blogger if you happen to are not already.
    Cheers!

  13. Andyfreund.de表示:

    When some one searches for his vital thing, so he/she desires to
    be available that in detail, so that thing is maintained over
    here. https://Andyfreund.de/wiki/index.php?title=Comptoir_En_Granit_%C3%83_Mascouche_:_Caract%C3%83_ristiques_Et_Avantages

  14. ClydePab表示:

    matadorbet giris: matadorbet.bid – matadorbet.bid

  15. Sazrrbt表示:

    Можно ли купить аттестат о среднем образовании, основные моменты и вопросы
    vv.flybb.ru/viewtopic.php?f=23&t=2537

  16. LarryCoell表示:

    Casino Siteleri Casino Siteleri casino siteleri win

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

  18. Feel free to visit my web blog – Daycares popular listings (http://kwar.net)

  19. LarryCoell表示:

    ultrabet ultrabet ultrabet giris

  20. LarryCoell表示:

    ultrabet ultrabet ultrabet tr online

  21. zapada01表示:

    Hi there, You’ve done a fantastic job. I’ll definitely digg it and personally suggest to my friends.
    I’m sure they’ll be benefited from this website.

    Take a look at my site :: zapada01

  22. ClydePab表示:

    ultrabet tr online: ultrabet giris – ultrabet

  23. LarryCoell表示:

    ultrabet ultrabet ultrabet bonus

  24. You’ll Be Unable To Guess Self Propelled Wheelchairs For Sale’s
    Benefits self Propelled wheelchairs

  25. Leanna表示:

    Five Killer Quora Answers On Cheap Bunk Beds
    For Kids cheap bunk beds for kids [Leanna]

  26. Car表示:

    14 Cartoons About Audi Key Replacement That’ll Brighten Your Day
    Car

  27. Derekfar表示:

    az parayla cok kazandiran slot oyunlar?: slot oyunlar? – az parayla cok kazandiran slot oyunlar?

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

  29. Derekfar表示:

    ultrabet guncel: ultrabet guncel – ultrabet yeni giris 1125

發佈留言

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