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,195,450 Responses

  1. Senaida表示:

    A Guide To Veterans Disability Lawsuit In 2023 veterans disability law
    firm (Senaida)

  2. A Few Practical Things On Obtaining A Renewed
    Car Finance 신생아 특례 대출

  3. Rvjqdn表示:

    order durex gel online cheap – buy zovirax cheap where to buy xalatan without a prescription

  4. neucapone.com表示:

    How Do Those “Fast” Backlink Services Work? 구글SEO (neucapone.com)

  5. When I initially left a comment I appear to have clicked the
    -Notify me when new comments are added- checkbox and now every time a comment is added I recieve
    four emails with the exact same comment. There has to be a means you are able to
    remove me from that service? Cheers!

  6. The 10 Most Scariest Things About Upvc Repairs Near
    Me Upvc repairs near Me

  7. 구글SEO表示:

    Webinar On Seo By Susanne Myers: Seo Strategies That Work 구글SEO

  8. What’s The Reason Everyone Is Talking About Medical Malpractice Lawsuit Right Now medical malpractice law firm

  9. betflik 8k表示:

    It’s perfect time to make a few plans for the longer term and it’s time to be
    happy. I have learn this submit and if I may just I desire to counsel you few attention-grabbing issues or advice.
    Maybe you can write subsequent articles relating to this article.
    I desire to learn more issues approximately it!

  10. Medicine information sheet. What side effects?
    where can i get arimidex online
    Everything news about medicament. Get information now.

  11. The 10 Scariest Things About Double Glazing Company Near Me Double Glazing Company Near Me

  12. elsycrays表示:

    The Three Greatest Moments In Locksmiths Near Me For Car History elsycrays

  13. Hello, I enjoy reading through your article. I wanted to write a little comment to support you.

  14. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment
    is added I get several e-mails with the same comment.
    Is there any way you can remove people from that service?

    Cheers!

  15. link bokep表示:

    I love it when people come together and share views.
    Great site, stick with it!

  16. The Spare Car Key Cut Awards: The Most Stunning, Funniest, And Strangest Things
    We’ve Ever Seen http://www.99811760.xyz

  17. Attorneys表示:

    Why Do So Many People Want To Know About Veterans Disability Case?
    Attorneys

  18. Drugs prescribing information. What side effects?
    where to get zanaflex prices
    All news about medicament. Read information here.

  19. See What Replacement Double Glazed Glass Only Near Me Tricks The Celebs Are Using replacement double glazed glass Only

  20. It’s remarkable in support of me to have a site, which
    is useful for my experience. thanks admin

  21. What Is Veterans Disability Attorney? Heck What Is Veterans Disability Attorney?
    veterans disability lawsuits

  22. I love looking through a post that can make men and women think.
    Also, thank you for permitting me to comment!

  23. Цигун表示:

    I truly love your blog.. Pleasant colors & theme. Did you make this amazing
    site yourself? Please reply back as I’m wanting to create my very
    own site and want to learn where you got this
    from or what the theme is called. Appreciate it!

  24. I am extremely inspired together with your writing abilities as smartly as with the
    format to your weblog. Is that this a paid theme or did you modify it yourself?
    Anyway stay up the nice quality writing, it is uncommon to look a
    great weblog like this one nowadays..

  25. mournheim.com表示:

    See What Double Glazing Repairs Near Me Tricks The Celebs Are Using double glazing repairs near me; mournheim.com,

發佈留言

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