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,229,001 Responses

  1. Davidtes表示:

    ciprofloxacin 500mg buy online ciprofloxacin 500 mg tablet price ciprofloxacin mail online

  2. Janis表示:

    The Reason Why Adding A Bunk With Double Bed To Your Life Can Make All The A Difference quadruple bed (Janis)

  3. training表示:

    This Is The Ultimate Guide To Folding Treadmill training

  4. 10 Things You Learned In Preschool That’ll Help You Understand Local
    Locksmith For Cars nearest locksmith For cars

  5. Colin表示:

    10 Tips For Quickly Getting Ford Key Repair ford car key replacement near
    me (Colin)

  6. Davidtes表示:

    cost generic propecia pills order cheap propecia pill cost of generic propecia

  7. 15 Up-And-Coming Mesothelioma Law Bloggers You Need To Check Out mesothelioma case
    (http://0522224528.Ussoft.kr/)

  8. RandallTaulp表示:

    ciprofloxacin 500 mg tablet price: buy cipro online – buy cipro

  9. Davidtes表示:

    ciprofloxacin 500 mg tablet price buy cipro cheap antibiotics cipro

  10. If some one wishes to be updated with latest technologies therefore he must be pay a quick visit this site and be up to date daily.

  11. Davidtes表示:

    antibiotics cipro buy generic ciprofloxacin ciprofloxacin

  12. I needed to thank you for this good read!! I absolutely
    enjoyed every bit of it. I have you book marked to look
    at new stuff you post…

  13. montok表示:

    That is very fascinating, You are a very professional blogger.
    I’ve joined your feed and sit up for searching for more of your great post.
    Also, I have shared your website in my social networks

  14. WilliamRhype表示:

    http://finasteride.store/# buy propecia for sale

  15. Door repair表示:

    Misted Double Glazing Repairs Tips That Can Change Your Life Door repair

  16. 5 Double Glazing Repairs Near Me Projects That Work
    For Any Budget Replacement Double Glazed Glass Only

  17. The 10 Scariest Things About Replacement Key
    For Mini Cooper replacement Key for mini cooper

  18. WilliamRhype表示:

    https://lisinopril.network/# where to buy lisinopril without prescription

  19. Candida表示:

    15 Latest Trends And Trends In Under Counter Side By Side Fridge Freezer 90cm which brand has the best side by side refrigerator (Candida)

  20. 15 Surprising Facts About Bunk Beds For Kids cheap bunk beds for kids

  21. You’ll Never Guess This Double Glazing Doors Repairs’s Secrets upvc door repairs Near me

  22. I get pleasure from, cause I found exactly what I was taking a look for.
    You’ve ended my 4 day long hunt! God Bless you man. Have a great
    day. Bye

  23. window repair表示:

    You’ll Never Be Able To Figure Out This Window Repair
    Near’s Tricks window repair

  24. What Is The Secret Life Of Locksmith Cars Nearest car locksmith

  25. WilliamRhype表示:

    https://cytotec.club/# buy cytotec

  26. Davidtes表示:

    tamoxifen chemo nolvadex vs clomid tamoxifen breast cancer

  27. Nine Things That Your Parent Teach You About Mesothelioma Litigation mesothelioma litigation – Web011.dmonster.kr,

  28. Don’t Be Enticed By These “Trends” About Locksmiths Near
    Me Auto cheapest Auto locksmith

  29. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 40 mg brand name

  30. 10 Best Mobile Apps For ADHD Testing Adult Test For Adhd

發佈留言

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