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,521,325 Responses

  1. The very best CBD products are made from high-quality hemp bacon flavored cbd oil for dogs.

  2. NewULife Ranks. Invite to my NewUlife Testimonial.

    my web blog heavy duty truck parts san antonio tx

  3. Ricardoverve表示:

    подробнее здесь 1хслотс

  4. The best bacon flavored cbd oil for dogs items are made from
    top notch hemp CBD.

  5. I always was concerned in this topic and still am,
    thank you sexual assault lawyers for victims putting up.

  6. No infrared Sauna room wood lasts longer than cedarwood.

  7. Essentially Equity Release is a long term loan.

    Feel free to surf to my blog … lifetime mortgage lending llc

  8. A synthetic USP human development hormonal agent
    (somatropin).

    Also visit my website – ai detector tool grammarly

  9. The most effective cbd oil for cats products are
    made from top quality hemp CBD.

  10. They have items to market or solutions to use.

    Also visit my webpage :: heavy Duty truck Parts texas

  11. Well I truly enjoyed reading it. This information offered by you is very constructive for proper planning.

    Review my blog … sexual harassment lawyers near me

  12. I always was interested in this topic and stock still am, thank you for posting.

    My web site :: best sexual harassment Lawyers Nyc

  13. They have items to sell or solutions to provide.

    my blog post – heavy duty truck parts midland tx

  14. No infrared sauna room cost timber lasts longer than cedarwood.

  15. USP human development hormone (somatropin).

    Feel free to surf to my webpage: semi truck Supply store near me

  16. NewULife Ranks. Welcome to my NewUlife Evaluation.

    Here is my web-site – Heavy Duty Truck Parts

  17. In this case, the life insurance policy firm’s loan.

    My web blog :: ai detector for essays

  18. Genuine HGH Rewards (NewULife Hgh Gel Not Examined Yet).

    my web blog … Ai detector tool Free

  19. Way cool! Some very valid points! I appreciate you writing this post
    plus the rest of the site is very good.

    Also visit my webpage sexual assault victim lawyers Near me

  20. Система лояльности в онлайн Лев казино зеркало вознаграждает активных игроков

  21. What is New U Life SOMADERM Transdermal Gel.

    Feel free to surf to my page … heavy duty semi truck parts store near me

  22. NewULife Ranks. Welcome to my NewUlife Testimonial.

    my site; used semi truck parts store near me

  23. NewULife Ranks. Invite to my NewUlife Review.

    Feel free to visit my homepage: heavy duty semi truck parts store near me

發佈留言

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