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,367,628 Responses

  1. Excellent post. I was checking constantly this weblog
    and I am inspired! Very useful info specially the remaining section :
    ) I take care of such info a lot. I was seeking this certain info for a very long time.
    Thanks and best of luck.

    Here is my web page :: is lung clear pro legit

  2. RamonRek表示:

    buy cheap ozempic: buy ozempic pills online – buy ozempic

  3. Rozella表示:

    Why You’ll Definitely Want To Find Out More About Car Diagnostic Near Me engine diagnostics
    [Rozella]

  4. HaroldBig表示:

    Ozempic without insurance: Ozempic without insurance – buy ozempic pills online

  5. Inviertas表示:

    I am actually thankful to the holder of this site who has shared this fantastic article at at this time.

    My page: Inviertas

  6. The Unknown Benefits Of Pragmatic 프라그마틱

  7. RichardGed表示:

    betting sign up promos: A code that unlocks a free bet on a betting site, allowing users to place a wager without using their own funds, often as part of a promotion.

  8. The Most Common Mistakes People Make Using Fireplace Surround wood fireplaces

  9. BarryDof表示:

    https://ozempic.art/# buy ozempic pills online

  10. Сервисный центр предлагает выездной ремонт холодильников indesit центр ремонта холодильника indesit

  11. Magnificent beat ! I wish to apprentice at the same time as you amend your web site, how could i subscribe for a blog site?

    The account aided me a applicable deal. I had been a little bit
    familiar of this your broadcast provided vibrant transparent concept

  12. Jeremypag表示:

    buy ozempic pills online ozempic buy ozempic pills online

  13. BarryDof表示:

    https://ozempic.art/# buy ozempic

  14. Frankevild表示:

    https://rybelsus.shop/# rybelsus pill

  15. megaweb at表示:

    Good article! We are linking to this particularly great content on our
    website. Keep up the great writing.

    Also visit my blog: megaweb at

  16. RamonRek表示:

    semaglutide online: semaglutide online – buy semaglutide pills

  17. An intriguing discussion is definitely worth comment.
    I do believe that you need to publish more on this subject matter, it may not be a
    taboo subject but usually folks don’t discuss such issues.
    To the next! Kind regards!!

  18. HaroldBig表示:

    semaglutide online: rybelsus coupon – rybelsus price

  19. Jeremypag表示:

    ozempic buy ozempic ozempic generic

  20. Профессиональный сервисный центр по ремонту электросамокатов в Москве.
    Мы предлагаем: где починить электросамокат в москве
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  21. Bean To Cup Coffee Makers Explained In Less Than 140
    Characters Bean To Cup Or Espresso Machine

  22. 20 Pragmatic Ranking Websites Taking The Internet By Storm 프라그마틱 공식홈페이지

  23. studio表示:

    Hey! Someone in my Facebook group shared this website with us so I came to take a look.
    I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers!
    Fantastic blog and excellent design.

  24. When I initially commented I clicked the “Notify me when new comments are added”
    checkbox and now each time a comment is added I get four emails with the same comment.
    Is there any way you can remove me from that
    service? Bless you!

  25. RamonRek表示:

    semaglutide cost: rybelsus pill – rybelsus price

  26. I’m not that much of a internet reader to be honest but
    your sites really nice, keep it up! I’ll go ahead and bookmark your site
    to come back later. Cheers

  27. Frankevild表示:

    https://ozempic.art/# ozempic generic

  28. BarryDof表示:

    https://rybelsus.shop/# rybelsus cost

發佈留言

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