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,231,519 Responses

  1. 15 Of The Best Pinterest Boards Of All Time About Online Shopping Stores List Large Wall Art Frame 19X29

  2. RandallTaulp表示:

    order cheap propecia: order cheap propecia without dr prescription – order propecia without rx

  3. RandallTaulp表示:

    cipro generic: cipro pharmacy – ciprofloxacin over the counter

  4. Ronaldpsymn表示:

    http://cytotec.club/# Cytotec 200mcg price

  5. 9 . What Your Parents Taught You About Double Glazed Windows
    Near Me double glazed windows near me

  6. RandallTaulp表示:

    lisinopril 40 mg canada: zestril online – lisinopril 10 mg brand name in india

  7. 16 Facebook Pages That You Must Follow For Upvc Window Repairs-Related Businesses window repairs Near me

  8. WilliamRhype表示:

    http://nolvadex.life/# does tamoxifen make you tired

  9. door repair表示:

    The 12 Worst Types Of Accounts You Follow On Twitter door repair

  10. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# ciprofloxacin order online

  11. RandallTaulp表示:

    lisinopril 50 mg: lisinopril 20 mg sale – purchase lisinopril 10 mg

  12. Robinguery表示:

    Рубероид РКК – это высококачественный материал, отвечающий всем требованиям госта. Он имеет высокую прочность, вес и габариты, а также теплостойкость до 80 градусов Пергамин П 250 ТУ (15 кв. м.)

  13. Davidtes表示:

    buy misoprostol over the counter buy cytotec buy cytotec over the counter

  14. Arturo表示:

    16 Facebook Pages That You Must Follow For Handbag Tommy Hilfiger Marketers tommy hilfiger tote
    bag, Arturo,

  15. Davidtes表示:

    ciprofloxacin 500mg buy online ciprofloxacin 500mg buy online п»їcipro generic

  16. 14 Questions You’re Anxious To Ask Double Glazed Window Repairs Near Me Double Glazed Window Repairs

  17. RandallTaulp表示:

    buy cytotec over the counter: buy cytotec over the counter – purchase cytotec

  18. 5 Laws That Can Help Industry Leaders In Slot Payouts Industry Casino Slot Tournaments

  19. RandallTaulp表示:

    cost of generic propecia price: buy generic propecia without insurance – buying generic propecia price

  20. Davidtes表示:

    cytotec buy online usa п»їcytotec pills online Cytotec 200mcg price

  21. WilliamRhype表示:

    http://lisinopril.network/# cheap lisinopril no prescription

  22. LCK토토表示:

    I was more than happy to discover this page. I need to to thank you for ones time for this particularly fantastic
    read!! I definitely enjoyed every bit of it and i also
    have you bookmarked to look at new stuff in your site.

  23. Asking questions are genuinely good thing if you are not understanding anything completely,
    except this article provides good understanding yet.

  24. Ronaldpsymn表示:

    https://nolvadex.life/# nolvadex steroids

  25. Davidtes表示:

    tamoxifen warning tamoxifen rash is nolvadex legal

  26. local表示:

    How To Explain Volkswagen Keys To A 5-Year-Old local

  27. A Comprehensive Guide To Drip Filter. Ultimate Guide To Drip Filter
    coffee filter Machine

  28. Vimeo表示:

    15 Reasons Why You Shouldn’t Overlook Cheap Online Grocery Shopping Uk Vimeo

  29. google.Com.co表示:

    Why Everyone Is Talking About Online Home Based Work Right Now work from home no experience,
    google.Com.co,

發佈留言

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