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,364,354 Responses

  1. It Is Also A Guide To Cheap Nespresso Machine In 2023 nespresso Vertuo Next coffee machine

  2. Cs20 Case表示:

    5 Laws That Will Help To Improve The CSGO Battle Case
    Industry Cs20 Case

  3. naga169表示:

    My developer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the costs. But he’s tryiong none the less.

    I’ve been using WordPress on various websites for about a
    year and am anxious about switching to another platform.
    I have heard very good things about blogengine.net.
    Is there a way I can transfer all my wordpress posts into it?
    Any help would be really appreciated!

  4. 15 Things You’re Not Sure Of About Window Repair Near window repair Near Me

  5. Mora表示:

    15 Things You’re Not Sure Of About Replacement Window Handle upvc replacement window handles (Mora)

  6. Five Killer Quora Answers To Double Glazed Near Me
    double glazed near me

  7. Some Wisdom On ADHD Diagnosis Adults From The Age Of Five adhd diagnosis for adults

  8. My brother recommended I might like this blog. He was once totally right.
    This publish truly made my day. You can not believe
    just how much time I had spent for this information! Thank you!

  9. Why Is Automatic Folding Mobility Scooter Near Me So Popular?
    auto Fold mobility Scooter (http://www.diggerslist.com)

  10. lawyer表示:

    The Malpractice Litigation Awards: The Best, Worst
    And Strangest Things We’ve Ever Seen lawyer

  11. 14 Common Misconceptions Concerning Mesothelioma Attorney
    Mesothelioma Case

  12. Gummies表示:

    Woah! I’m really loving the template/theme of
    this website. It’s simple, yet effective. A lot of times it’s
    challenging to get that “perfect balance” between superb usability and appearance.
    I must say you’ve done a great job with this. In addition, the blog loads very quick for me on Internet explorer.
    Superb Blog!

  13. Hi there! I understand this is kind of off-topic however I
    needed to ask. Does operating a well-established website like yours require a large amount of work?
    I’m brand new to running a blog but I do write in my journal daily.
    I’d like to start a blog so I can easily share my
    own experience and views online. Please let me know if you have any kind of recommendations or
    tips for new aspiring blog owners. Appreciate it!

  14. What’s The Current Job Market For Kids Triple Bunk Beds Professionals?
    Kids Triple bunk beds

  15. Where To Research Double Mattress Online Firm Double Mattress

  16. Good day! This post could not be written any better! Reading through this post
    reminds me of my previous room mate! He always kept talking about this.
    I will forward this page to him. Fairly certain he will have a good read.
    Many thanks for sharing!

  17. What Is Auto Locksmith Near Me And Why Is Everyone Dissing It?
    auto locksmith key Programming

  18. Heya! I understand this is somewhat off-topic however I needed to ask.

    Does building a well-established website such as yours require a lot
    of work? I am completely new to writing a blog but I do write in my
    diary everyday. I’d like to start a blog so I can share my own experience and thoughts online.

    Please let me know if you have any kind of recommendations or tips for new
    aspiring blog owners. Appreciate it!

  19. See What Veleco Mobility Scooter For Sale Tricks The Celebs Are Making Use Of veleco mobility Scooter

  20. See What Replace Volvo Key Tricks The Celebs Are Utilizing Replace volvo key

  21. The 10 Scariest Things About Link Alternatif Gotogel Link
    Alternatif gotogel [0522224528.ussoft.Kr]

  22. Hand bag表示:

    The Most Significant Issue With Prada Man Handbag And How You Can Resolve
    It Hand bag

  23. Pretty nice post. I just stumbled upon your blog and wanted to say that I have really
    enjoyed browsing your blog posts. After all I will be subscribing to your feed and I
    hope you write again soon!

  24. Leonard表示:

    Why Is Double Mattresses Memory Foam So Famous? double bed mattress price (Leonard)

  25. Do you have a spam issue on this website; I also am a blogger, and I was wanting to know your situation; many of
    us have created some nice methods and we are looking to swap strategies with other folks, please shoot me an e-mail if interested.

  26. Great beat ! I wish to apprentice whilst you amend your web site,
    how can i subscribe for a weblog site? The account
    helped me a appropriate deal. I had been tiny bit familiar of this your
    broadcast offered vibrant transparent idea

  27. espresso pads表示:

    Just want to say your article is as surprising. The clarity in your put up is simply
    cool and that i could think you’re a professional in this
    subject. Fine together with your permission allow me to clutch your feed to keep updated
    with imminent post. Thank you 1,000,000 and please continue the enjoyable
    work.

  28. What’s The Current Job Market For Veleco Mobility Scooters Uk Professionals Like?

    Veleco mobility Scooters

發佈留言

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