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,266,392 Responses

  1. Nikefut表示:

    how to access dark web darknet drugs deep web drug url

  2. Amazing! This blog looks just like my old one!
    It’s on a entirely different topic but it has pretty much the same layout and design. Superb choice of colors!

  3. Simply want to say your article is as amazing. The clearness in your
    put up is simply cool and i could assume you’re a professional on this subject.
    Fine together with your permission let me to take hold of your RSS feed to keep
    updated with impending post. Thanks a million and please continue the enjoyable work.

  4. CurtisIncix表示:

    вавада казино: вавада – вавада казино

  5. Lura Beck表示:

    Finally got rid of all that clutter thanks to Eco-friendly dumpster rental Orlando

  6. Elmer Sanchez表示:

    I recently had my screen repaired by All Screening of SWFL Pool Screen Repair Cape Coral

  7. Jamesdiure表示:

    pinup-kazi.ru: pinup – pinup

  8. I’m curious to find out what blog system you are utilizing?

    I’m having some minor security issues with my latest website and I would like to find something more safe.
    Do you have any solutions?

  9. Owen Hopkins表示:

    Your examples of efficient hardscapes without a doubt motivate me to get began by myself lawn project! excavation companies Concord

  10. HenrydeT表示:

    pin up казино pinup-kazi.kz пин ап казино

  11. DarioTob表示:

    vavada: вавада – vavada-kazi.ru

  12. Hi there I am so happy I found your web site, I really found you by accident,
    while I was browsing on Google for something else, Anyways I am here now and would just like to say
    kudos for a marvelous post and a all round interesting blog
    (I also love the theme/design), I don’t have time to look over it all at the
    minute but I have bookmarked it and also added your RSS feeds,
    so when I have time I will be back to read more, Please do keep up the
    great work.

  13. Thanks for the marvelous posting! I really enjoyed reading it, you will be a great author.I
    will make sure to bookmark your blog and definitely will come back later on.
    I want to encourage yourself to continue your great posts,
    have a nice weekend!

  14. DarioTob表示:

    pinup-kazi.ru: пин ап казино официальный сайт – пин ап вход

  15. CurtisIncix表示:

    пин ап кз: пин ап казино онлайн – pinup kazi

  16. you are actually a excellent webmaster. The web site loading
    velocity is amazing. It sort of feels that you’re
    doing any distinctive trick. In addition, The contents are masterpiece.
    you have performed a excellent job in this subject!

  17. POLTMLR57表示:

    An outstanding share! I have just forwarded this onto a co-worker who has been conducting a little research on this.
    And he actually bought me breakfast due to the fact that I discovered it for
    him… lol. So let me reword this…. Thank YOU for the
    meal!! But yeah, thanx for spending the time to talk about this subject here
    on your internet site.

  18. DarioTob表示:

    вавада: вавада онлайн казино – vavada

  19. Josephmax表示:

    https://pinup-kazi.kz/# пин ап казино

  20. Every weekend i used to pay a visit this website,
    because i wish for enjoyment, for the reason that this
    this web page conations really fastidious funny material too.

  21. Iyannaobesk表示:

    джекпокер рум
    играть на джекпокер

  22. HenrydeT表示:

    pinup-kazi.kz pinup-kazi.kz pinup-kazi.kz

  23. Excellent blog here! Also your website loads up fast!

    What host are you using? Can I get your
    affiliate link to your host? I wish my site loaded up
    as quickly as yours lol

  24. HenrydeT表示:

    pinup-kazi.ru pinup пин ап зеркало

  25. Iyannaobesk表示:

    jack poker freeroll password
    freeroll password jack poker

  26. Being bound by rules, regular casinos restrict what players can do on their
    online platforms.

  27. GavinGen表示:

    Medication effects explained. Medication guide available.
    sumatriptan pneumonia
    Drug facts here. Access medication details.

發佈留言

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