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,363,098 Responses

  1. StevenNub表示:

    prednisone 50 mg coupon: Prednisone Without Prescription – buy prednisone nz

  2. It’s awesome in favor of me to have a website, which is valuable in favor of my know-how.
    thanks admin

  3. Hmm it appears like your blog ate my first
    comment (it was super long) so I guess I’ll just sum it up what I had
    written and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog writer but I’m still new to
    the whole thing. Do you have any points for beginner blog
    writers? I’d genuinely appreciate it.

  4. best ai tool表示:

    This is the right site for anyone who would like to understand this topic.
    You know so much its almost tough to argue with you (not that I actually will need to…HaHa).
    You certainly put a brand new spin on a topic that’s been discussed
    for many years. Wonderful stuff, just wonderful!

    Look into my site – best ai tool

  5. Ellen Gardner表示:

    Thanks for the great tips. Discover more at tire change at home

  6. Excellent write-up. I certainly love this website. Thanks!

  7. Hi to every body, it’s my first pay a visit of this blog;
    this weblog carries amazing and truly good stuff in favor of visitors.

  8. Dereketerm表示:

    max pharm: priligy maxpharm – dapoxetine online

  9. врач нарколог на дом платный motik13.0pk.me/viewtopic.php?id=1995 .

  10. StevenNub表示:

    how to get amoxicillin over the counter: Amoxicillin for sale – amoxicillin 500mg buy online canada

  11. If you are going for finest contents like me, simply pay a visit this web site every day
    as it presents quality contents, thanks

  12. You Will Meet The Steve Jobs Of The Metal Anal Butt Plug
    Industry Butt Plug Anal

  13. 20 Rising Stars To Watch In The Car Replacement Key Cost Industry cheapest replacement car keys

  14. it’s already getting even easier to get there,thanks to Delta Air Lines’ brand-new routes from Hawaii to Tokyo plus flights that resumed this fall from Los Angeles.セクシー下着

  15. Sazrepr表示:

    Сколько стоит получить диплом высшего и среднего образования легально?

    oldforum.citysakh.ru/?talkid=27665

  16. TOGEL ONLINE表示:

    You have amazing leadership qualities
    TOGEL ONLINE

  17. StevenNub表示:

    cheap priligy: dapoxetine price – priligy max pharm

  18. I know this site provides quality based content and other information, is there
    any other site which gives these kinds of stuff in quality?

    Feel free to visit my web blog: random video chat

  19. BrandenSlomy表示:

    priligy maxpharm: max pharm – buy priligy max pharm

  20. Nikefut表示:

    tor marketplace deep web search https://darknetmarketstore.com/ – free dark web

  21. I got this web page from my pal who informed me about this website and at
    the moment this time I am visiting this site and reading very informative
    articles or reviews at this time.

  22. Attractive section of content. I just stumbled upon your website
    and in accession capital to assert that I get in fact enjoyed account your blog posts.

    Any way I’ll be subscribing to your feeds and even I achievement you access consistently quickly.

  23. Superb blog! Do you have any tips and hints for aspiring writers?
    I’m planning to start my own site soon but I’m
    a little lost on everything. Would you advise starting with a
    free platform like WordPress or go for a paid
    option? There are so many choices out there that I’m
    completely confused .. Any tips? Thanks!

  24. Sazralx表示:

    Как быстро и легально купить аттестат 11 класса в Москве

    magazin.orgsoft.ru/communication/forum/index.php?PAGE_NAME=profile_view&UID=208216

  25. Meds prescribing information. Drug Class.
    can i order paxil without dr prescription
    Best news about medicament. Read here.

  26. Asbestos law表示:

    Mesothelioma is a cancer of the lining of the lungs (pleura) as well
    as the tummy (peritoneal mesothelioma) and the heart.
    The symptoms typically develop in the course of time.

    Here is my blog post Asbestos law

  27. StevenNub表示:

    buy amoxicillin: cheap amoxil – amoxicillin over the counter in canada

發佈留言

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