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,244,464 Responses

  1. great submit, very informative. I ponder why the other experts of this sector do not understand this. You must proceed your writing. I am confident, you’ve a huge readers’ base already!

  2. Good information. Lucky me I discovered your website by accident (stumbleupon).
    I’ve bookmarked it for later!

  3. kokitoto表示:

    Wow, incredible blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your web site is great, as well as the content!

  4. Lazrfpu表示:

    Сколько стоит диплом высшего и среднего образования и как его получить?
    registraciavsaita.listbb.ru/viewtopic.php?f=2&t=1269

  5. We stumbled over here by a different page and thought I should check things out.
    I like what I see so now i am following you.
    Look forward to going over your web page repeatedly.

  6. Best Apps to Make Money in Pakistan, Simple Ways to Make Money in Pakistan Through an App, For Anyone Who Wants to Make Money, Effective Ways to Make Money in Pakistan Through Apps, Popular Apps to Make Money in Pakistan, Earning money in Pakistan using applications: is it real?, which you need to know, which do not violate the law, for those who strive for financial independence, Interesting platforms for making money in Pakistan, Successful ways to make money in Pakistan, Promising apps for making money in Pakistan, The most interesting apps for making money in Pakistan, which will lead to financial independence, Optimal platforms for making money in Pakistan, for making money at home, which bring real money, to increase incomebest online earning websites in pakistan how to earn money online in pakistan for students .

  7. Lazryec表示:

    Приобретение диплома ПТУ с сокращенной программой обучения в Москве
    mireait.listbb.ru/viewtopic.php?f=2&t=715

  8. Nikefut表示:

    dark web markets deep web markets https://darknetmarketstore.com/ – darknet marketplace

  9. If you want to get a good deal from this paragraph then you have to
    apply these strategies to your won webpage.

  10. I pay a visit day-to-day some blogs and blogs to read posts, however this weblog presents quality based
    posts.

  11. Lazrwqr表示:

    Как приобрести диплом о среднем образовании в Москве и других городах

    actionroleplay.forumex.ru/viewtopic.php?f=26&t=154

  12. olympine表示:

    Plongez dans des énigmes captivantes et uniques, adaptées à l’âge
    et aux intérêts de chaque enfant.

  13. My relatives always say that I am killing my time here at web, but
    I know I am getting know-how every day by reading thes fastidious articles or reviews.

  14. Hello there, just became alert to your blog
    through Google, and found that it is really informative.
    I’m going to watch out for brussels. I will be grateful if
    you continue this in future. A lot of people will be benefited
    from your writing. Cheers!

  15. koitoto表示:

    Hello There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and come back to read more
    of your useful info. Thanks for the post. I will certainly return.

  16. Hello very nice web site!! Guy .. Excellent .. Amazing ..
    I will bookmark your website and take the feeds also? I’m happy to search out so many useful information here in the publish, we’d like develop more strategies on this regard, thank you for sharing.

    . . . . .

  17. Pretty! This was a really wonderful article.
    Many thanks for providing this info.

  18. Meds information for patients. Short-Term Effects.
    order benicar price
    Everything what you want to know about medicine. Get information now.

  19. Hello, its nice article regarding media print, we all know media is a wonderful source of data.

  20. Hey There. I found your weblog the use of msn. That is
    a really neatly written article. I will be sure to bookmark it and come back to learn extra of your helpful info.

    Thank you for the post. I will definitely return.

發佈留言

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