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

  1. Узнай все о выпрямление носовой перегородкиискривление перегородки носа

  2. slot mpo表示:

    If you wish for to increase your familiarity only keep
    visiting this web page and be updated with the most recent information posted
    here.

  3. Cet article est une mine d’or pour tout artisan voulant se digitaliser.
    Des conseils pratiques et pertinents!

  4. If you wish for to take a great deal from this paragraph then you have to
    apply such methods to your won web site.

  5. Hey there! This is my first visit to your blog!

    We are a collection of volunteers and starting a new project in a community
    in the same niche. Your blog provided us valuable information to work
    on. You have done a marvellous job!

  6. AlbertDuM表示:

    buy plavix: п»їplavix generic – buy Clopidogrel over the counter

  7. AlbertDuM表示:

    plavix medication: buy plavix online – Clopidogrel 75 MG price

  8. I was extremely pleased to discover this great site. I want to to thank you for ones time for this fantastic read!!
    I definitely enjoyed every part of it and I
    have you book-marked to see new information on your blog.

    Also visit my web page بهترین بروکر فارکس

  9. rtp slot live表示:

    After I initially left a comment I seem to have clicked on the -Notify me when new comments are added-
    checkbox and now each time a comment is added I recieve four emails
    with the exact same comment. Is there a way you are able to remove me from that service?
    Many thanks!

  10. find more表示:

    I have been browsing online more than three hours today, yet
    I never found any interesting article like yours. It is pretty worth enough for me.
    Personally, if all webmasters and bloggers made good content as you did,
    the net will be much more useful than ever before.

  11. BernardMop表示:

    Plavix generic price buy Plavix Clo generic plavix

  12. BrandenSlomy表示:

    prednisone 5084: prednisone – prednisone for sale no prescription

  13. AlbertDuM表示:

    antiplatelet drug: Clopidogrel Best Prices – Cost of Plavix on Medicare

  14. BrandenSlomy表示:

    how to buy generic clomid no prescription: clomid – can i get clomid

  15. When you are looking for a poster of Kanye West, you will notice tons of stylish pieces.
    Whether it’s inspired by his legendary records like The Life of Pablo, to modern collections, there’s something that matches any music lover.

    Kanye West print definitely can enhance your interior.

  16. apkportalnik表示:

    закачать приложения онлайн казино https://atiragusa.it/kazino-bez-vlozhenij-2024-igrat-onlajn-v-igrovyh-7/

  17. Hey I know this is off topic but I was wondering if you knew of any widgets I
    could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time
    and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  18. AlbertDuM表示:

    discount zestril: cheapest Lisinopril – can you order lisinopril online

  19. Узнай все о операция на носовую перегородкуисправление носовой перегородки цена

發佈留言

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