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,244,572 Responses

  1. Aw, this was an extremely good post. Taking a few minutes and actual effort to generate a very good article… but what can I say… I hesitate a lot and never manage to get nearly anything done.

  2. срочная помощь вывод из запоя краснодар https://nsk.ukrbb.net/viewtopic.php?f=45&t=29670 .

  3. вывод из запоя на дому краснодар цены https://cah.forum24.ru/?1-19-0-00000461-000-0-0-1730745053/ .

  4. slot gacor表示:

    Can you tell us more about this? I’d love to find out
    more details.

  5. I have actually been looking into different SEO strategies, and I encountered Phoenix SEO Digitaleer Web Design

  6. Thanks for sharing your thoughts on web developers near
    me. Regards

  7. Boekp Viral表示:

    Definitely imagine that that you said. Your favourite reason appeared to be on the net the easiest factor to be mindful of.

    I say to you, I certainly get irked at the same time as other folks think about concerns
    that they just don’t understand about. You managed to hit the nail upon the top and
    outlined out the whole thing with no need side effect , other people could take a signal.
    Will probably be again to get more. Thanks

  8. rjp777表示:

    A motivating discussion is definitely worth comment.
    I do believe that you should publish more on this topic,
    it may not be a taboo matter but typically folks don’t talk about such
    topics. To the next! Many thanks!!

  9. вывод из запоя краснодар [url=http://www.zelenograd.rusff.me/viewtopic.php?id=1232]http://www.zelenograd.rusff.me/viewtopic.php?id=1232[/url] .

  10. вывод из запоя краснодар стационар belbeer.borda.ru/?1-6-0-00000757-000-0-0-1730745253 .

  11. принудительный вывод из запоя краснодар zal.rolevaya.info/viewtopic.php?id=5360 .

  12. DarrenUnjuh表示:

    https://iverfast.com/# stromectol south africa

  13. lonte malam表示:

    It’s actually very complicated in this active life to listen news on TV, therefore I
    only use internet for that reason, and get the latest news.

  14. вывод из запоя лечение краснодар tatuheart.ukrbb.net/viewtopic.php?f=8&t=15068 .

  15. My brother recommended I might like this web site.
    He was once totally right. This submit actually made my day.
    You can not believe just how so much time I had
    spent for this information! Thanks!

  16. money scam表示:

    Wow, this article is nice, my sister is analyzing these things, therefore
    I am going to let know her.

發佈留言

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