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,362,765 Responses

  1. BrandenSlomy表示:

    prednisone 5 mg brand name: ray pharm – prednisone prescription online

  2. StevenNub表示:

    prednisone 5 50mg tablet price: prednisone – prednisone 12 tablets price

  3. Nikefut表示:

    darknet market lists darknet links https://darknetmarketstore.com/ – dark market url

  4. Blakesmema表示:

    http://webcocktail.ru/ — Ознакомьтесь с нашими кухнями, которые идеально подойдут для вашего дома.

  5. Hello to all, it’s actually a good for me to visit this website, it consists of useful Information.

  6. Dereketerm表示:

    prednisone acetate: order Prednisone – how to get prednisone without a prescription

  7. BrandenSlomy表示:

    cheap clomid now: cheap clomid – cost cheap clomid online

  8. Hello there I am so excited I found your web site,
    I really found you by mistake, while I was looking
    on Askjeeve for something else, Nonetheless I am here now and
    would just like to say many thanks for a tremendous post
    and a all round entertaining blog (I also love the theme/design), I don’t have time
    to read it all at the moment but I have bookmarked it and also added
    in your RSS feeds, so when I have time I will be back to
    read much more, Please do keep up the excellent b.

  9. Dereketerm表示:

    get cheap clomid for sale: clomid – cost of clomid online

  10. Link exchange is nothing else except it is simply placing the other person’s web site link on your page at proper place and other person will also do same
    in favor of you.

  11. Quality posts is the secret to attract the viewers to pay a quick visit the website,
    that’s what this website is providing.

  12. WengToto表示:

    Hi, I ԁo ƅelieve this is an excellent website.
    I stumbledupon іt 😉 I may rethrn oncе agaіn since Ӏ saved as a favorite іt.

    Money and freedom is thе bеѕt way to ϲhange, may ʏou
    be rich and continue to һelp other people.

    Check οut my web site … WengToto

  13. вызов нарколога на дом круглосуточно https://www.motik13.0pk.me/viewtopic.php?id=1995 .

  14. Spectra Bot表示:

    Hey There. I found your blog using msn. This is an extremely smartly written article.
    I’ll make sure to bookmark it and come back to learn more of
    your helpful information. Thank you for the post.
    I will certainly comeback.

  15. Voce teve um sonho incomum?
    Olhar num espelho num sonho

    [url=https://waynecountynyjobs.com/2022/06/18/blog-digital-ocean/#comment-14019]Com que e que um elefante sonha?[/url] [url=https://dangerouscellphone.com/hello-world/#comment-251860]Sonho de um gato adormecido[/url] [url=https://tomachupicchutravel.com/salkantay-trek-on-your-own/#comment-34037]Num sonho, apanhar coisas[/url] [url=http://gawontech.com/bbs/board.php?bo_table=o_01&wr_id=47749]Com que e que um el[/url] [url=http://n51.com.sg/en/component/k2/item/1-fusce-euismod-tincidunt-purus-vitae]Sonha com um cao que morde o seu braco? Doeu e sangrou? Interpretacao de um sonho de acordo com diferentes livros de sonho[/url] 4d51_86

  16. Sazrtny表示:

    Купить диплом старого образца, можно ли это сделать по быстрой схеме?

    d91652pj.beget.tech/2024/10/20/poluchite-diplom-prosto.html

  17. I will right away grasp your rss feed as I can not find your e-mail subscription hyperlink or newsletter
    service. Do you’ve any? Please allow me understand
    so that I may just subscribe. Thanks.

    Visit my blog … random video chat

  18. StevenNub表示:

    prednisone generic brand name: Prednisone Without Prescription – prednisone 60 mg price

  19. Hello, this weekend is fastidious for me, since this point in time i am reading this great informative post here at my house.

發佈留言

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