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,238,083 Responses

  1. I am really enjoying the theme/design of your site. Do
    you ever run into any web browser compatibility problems?
    A number of my blog visitors have complained about my blog not
    operating correctly in Explorer but looks great in Chrome.
    Do you have any ideas to help fix this problem?

  2. LeroyBak表示:

    mexican rx online mexico pharmacy buying prescription drugs in mexico online

  3. It’s perfect time to make some plans for the future
    and it’s time to be happy. I have read this post and if I could I wish to suggest you few interesting things or tips.
    Perhaps you can write next articles referring to this article.
    I desire to read more things about it!

  4. Jorgeagopy表示:

    mexican rx online: mexico pharmacy – mexican mail order pharmacies

  5. Medication information leaflet. Drug Class.
    mirtazapine and delirium
    Actual trends of medicament. Get information now.

  6. BrandonSaisy表示:

    Online medicine home delivery: Indian pharmacy worldwide delivery – online shopping pharmacy india

  7. Diplomi_ypEn表示:

    купить диплом колледжа в омске orik-diploms.ru .

  8. LeroyBak表示:

    best online pharmacies in mexico mexican pharm 24 reputable mexican pharmacies online

  9. Diplomi_acEn表示:

    купить диплом 2024 orik-diploms.ru .

  10. Jorgeagopy表示:

    ed treatments: cheap meds – google viagra dosage recommendations

  11. Williamagric表示:

    ed vacuum pump https://mexicanpharm24.cheap/# best online pharmacies in mexico

  12. Melvinpah表示:

    Мойка помещения после пожара https://spec-uborka-posle-pozhara.ru/

  13. Lazrzin表示:

    Как получить диплом техникума с упрощенным обучением в Москве официально
    socialnetwork.cloudyzx.com/read-blog/46755

  14. 10 Double Glazing Windows Birmingham Tricks All Experts Recommend
    Double Glazing Repairs Birmingham

  15. Ten Taboos About Private Adhd Assessment Near Me You Shouldn’t Share
    On Twitter How Do I Get My Son Assessed For Adhd

  16. Greetings from Ohio! I’m bored to tears at work so I decided to browse your website on my
    iphone during lunch break. I love the knowledge you present
    here and can’t wait to take a look when I get home. I’m surprised at how fast your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyways, superb blog!

  17. A Complete Guide To Asbestos Cancer Law Lawyer Mesothelioma Settlement Dos And Don’ts
    mesothelioma Lawsuits

  18. LeroyBak表示:

    mexican pharmaceuticals online mexico pharmacy cheap mexican drugstore online

  19. Williamagric表示:

    buying ed pills online https://indianpharm24.pro/# indian pharmacy

  20. LeroyBak表示:

    mexico pharmacies prescription drugs mexican pharmacy mexican drugstore online

  21. DevonPhack表示:

    https://pharm24.pro/# ed treatment drugs
    ed treatment options

  22. Bandar Online Togel Techniques To Simplify Your Daily Lifethe
    One Bandar Online Togel Trick That Everyone Should Be Able To bandar online Togel

  23. LeroyBak表示:

    online drugs cheaper medications natural ed

發佈留言

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