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,674,221 Responses

  1. Marion表示:

    9 . What Your Parents Taught You About Sectional Sofas For Sale sofas for sale (Marion)

  2. Тут можно преобрести стоимость оружейного сейфа оружейный сейф интернет магазин

  3. philipb表示:

    I have been surfing on-line greater than three hours lately, but
    I never found any interesting article like yours.
    It is lovely price sufficient for me. Personally, if all web owners and bloggers made excellent content material as you probably did,
    the net will probably be much more helpful than ever before.

  4. Hi, i believe that i noticed you visited my web site thus i got here to go back the
    want?.I am trying to in finding issues to enhance my web site!I suppose its good enough to make use of a few of your ideas!!

  5. LarryCoell表示:

    matadorbet.bid matadorbet.bid matadorbet.bid

  6. Тут можно преобрести сейф для ружья купить оружейный сейф купить в москве

  7. Thank you a lot for sharing this with all folks you actually recognise what you are speaking about!
    Bookmarked. Please additionally consult with my site =).
    We may have a hyperlink alternate contract among us

  8. ClydePab表示:

    matadorbet: matadorbet – matadorbet bid

  9. ClydePab表示:

    en cok kazand?ran slot oyunlar?: slot tr online – slot oyunlar?

  10. forn star表示:

    What’s up, of course this piece of writing is actually fastidious and I have learned lot of things from it on the topic of blogging.
    thanks.

  11. CarrollUnton表示:

    deneme bonusu veren siteler yerliarama.org http://slot-tr.online/# slot oyunlar?

  12. Hi Dear, are you genuinely visiting this web site regularly, if so after that you will definitely
    get fastidious knowledge.

  13. WnrPaish表示:

    When you are dealing with a personal medical problem try buying ivermectin walgreens for your prescription.

  14. FVDAXCW表示:

    OKMGFTC OQDGOVG SEFQKIB IIUVOON
    https://9gm.ru/article?KYNWBY

  15. CarrollUnton表示:

    deneme bonusu veren siteler betturkey http://slot-tr.online/# en kazancl? slot oyunlar?

  16. Submitting feeds to RSS directories helps woth link
    indexing in a approach that pinging cannot. Thiis might
    bbe the quickest approach to get your internet pages listed.

    To begin with you miught properly be considering what
    all of the fuss is all abou with regards to indexing a person’s a technnique hyperlinks.
    The Internet has grown from just over an estimated 17,000 web sites in 2000 to
    properly over a billion now. An estimated 94% of B2B buyers and 81% of consumers
    currently usee online research whereas determining what product
    they want to purchase. Inside the marketinng sphere, an estimated 88%of B2B marketers and 76% of B2C marketers use
    content advertising and marketing. Which means so as to rank high on a
    search engine, an article will need to have significant content
    material. The subnission can be executed either manually or mechanically with the
    assistance of software program, though most
    engines like google favor the manual course of. It might detect the pattern of datta in net pages.
    website indexing

  17. Derekfar表示:

    deneme bonusu veren yeni siteler: deneme bonusu veren siteler mycbet.com – deneme bonusu veren siteler betturkey

  18. HassanbuifS表示:

    https://denemebonusuverensiteler.top/# deneme bonusu veren siteler yerliarama.org
    deneme bonusu veren siteler 2024

  19. You really make it seem really easy with your presentation but I in finding this matter to be really one thing that I think
    I might never understand. It kind of feels too complicated
    and extremely extensive for me. I’m looking
    ahead in your next submit, I will attempt to get the
    hold of it!

  20. CarrollUnton表示:

    denemebonusuverensiteler.top https://matadorbet.bid/# matadorbet

  21. HassanbuifS表示:

    http://denemebonusuverensiteler.top/# deneme bonusu veren siteler 2024
    deneme bonusu veren siteler betturkey betturkey.com

  22. Wow! Finally I got a blog from where I know how to
    actually take useful information regarding my study and
    knowledge.

    https://angkanet.uk/

  23. Cuantoto表示:

    Hello thеre! Tһis article cߋuldn’t Ƅe written any better!
    Looking at tһis article reminds mе of my previous roommate!

    He аlways кept talking about tһis. Ι most certainly ᴡill send thіs article tto һim.
    Fairly ϲertain hhe wiol һave а great read.
    Thank yoս for sharing!

    Feel free tοo surf to my web-site: Cuantoto

  24. LarryCoell表示:

    az parayla cok kazandiran slot oyunlar? slot oyunlar? puf noktalar? slot oyunlar? puf noktalar?

  25. Sallie表示:

    9 . What Your Parents Taught You About Mini Sectional Sofa mini sectional
    sofa (Sallie)

  26. ClydePab表示:

    matadorbet.bid: matadorbet giris – matadorbet

  27. Don’t Buy Into These “Trends” Concerning Retro Cream Fridge Freezer side by side fridge Freezer for open Kitchens (minecraftcommand.science)

  28. Derekfar表示:

    matadorbet giris: matadorbet bid – matadorbet giris

發佈回覆給「CarrollUnton」的留言 取消回覆

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