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,479,812 Responses

  1. B&B Italia表示:

    Can you tell us more about this? I’d like to
    find out more details.

  2. I love your blog.. very nice colors & theme. Did you
    make this website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to construct my own blog and would like
    to know where u got this from. thanks a lot

  3. Nikefut表示:

    tor markets links darknet market list deep web drug store

  4. Iyannaobesk表示:

    1 win poker скачать на русском
    1 win poker на андроид

  5. Piece of writing writing is also a fun, if you be familiar
    with afterward you can write or else it is complicated to write.

    Feel free to surf to my web site … bulk dvds for sale

  6. Jamesdiure表示:

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

  7. Silver water表示:

    Heya are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and set up my own. Do you require any coding expertise to make your own blog?
    Any help would be greatly appreciated!

  8. CurtisIncix表示:

    пинап казино: пин ап кз – пин ап кз

  9. Josephmax表示:

    http://vavada-kazi.ru/# vavada kazi

  10. I enjoyed this post. For additional info, visit Acupuncture Austin

  11. Max Long表示:

    Windows that sparkle can change the whole atmosphere of a house; I’ll definitely keep your tips h Residential Roof Cleaning

  12. Josephmax表示:

    http://vavada-kazi.ru/# vavada kazi

  13. Medicines information sheet. Effects of Drug Abuse.
    metformin more drug uses
    All information about pills. Get information now.

  14. I absolutely love your blog.. Pleasant colors & theme.

    Did you build this amazing site yourself? Please reply back as
    I’m attempting to create my very own blog and want to learn where you got this
    from or just what the theme is named. Cheers! https://Ecommk.com/question/top-paint-brands-in-canada-choosing-the-best-paints-for-your-projects-4/

  15. Jamesdiure表示:

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

  16. DarioTob表示:

    вавада: вавада казино зеркало – vavada kazi

  17. Howdy! This article couldn’t be written any better!
    Reading through this article reminds me of my previous roommate!
    He continually kept preaching about this. I am going to forward this information to him.
    Pretty sure he’ll have a very good read. Many thanks for sharing!

    Have a look at my site dvd distributors

  18. DarioTob表示:

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

  19. Howdy! I could have sworn I’ve visited your blog before but after browsing through many of the articles I
    realized it’s new to me. Anyways, I’m certainly happy I discovered it and I’ll be bookmarking it and checking back often!

  20. Sally Hill表示:

    Как вы относитесь к влиянию социальных сетей на SEO? Интересно ваше мнение по этому вопросу? seo оптимизация сайта

  21. DarioTob表示:

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

  22. I appreciated this article. For more, visit tire change at home

  23. DarioTob表示:

    пинап казино: пинап казино – пин ап казино

  24. You should be a part of a contest for one of the greatest sites online.
    I am going to highly recommend this web site!

發佈留言

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