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,480,445 Responses

  1. JerryMig表示:

    mexican pharmaceuticals online mexican pharmaceuticals online mexican pharmaceuticals online

  2. Louishycle表示:

    medicine in mexico pharmacies best mexican online pharmacies mexican pharmaceuticals online

  3. TravisNOb表示:

    https://mexicanph.com/# mexico drug stores pharmacies
    purple pharmacy mexico price list

  4. JulioAxowl表示:

    best online pharmacies in mexico mexican pharmacy mexican drugstore online

  5. JerryMig表示:

    pharmacies in mexico that ship to usa mexican drugstore online mexican mail order pharmacies

  6. JerryMig表示:

    mexican rx online mexican pharmaceuticals online mexican rx online

  7. EmilioGor表示:

    You actually said this terrifically.
    pharmacy cheap no prescription online pharmacies in usa pharmacy prices compare

  8. Louishycle表示:

    purple pharmacy mexico price list best mexican online pharmacies mexico pharmacies prescription drugs

  9. Asking questions are really good thing if you are not understanding anything
    fully, however this post provides fastidious understanding
    yet.

  10. JerryMig表示:

    buying prescription drugs in mexico online mexico pharmacies prescription drugs п»їbest mexican online pharmacies

  11. JerryMig表示:

    purple pharmacy mexico price list buying prescription drugs in mexico medicine in mexico pharmacies

  12. A motivating discussion is definitely worth comment. I do think that
    you ought to write more on this issue, it might not be a taboo matter but usually
    people do not discuss such subjects. To the
    next! Many thanks!!

  13. I’d like to thank you for the efforts you’ve put in writing this site.
    I really hope to see the same high-grade content from you
    later on as well. In fact, your creative writing abilities has motivated me
    to get my own site now 😉

  14. Hey great blog! Does running a blog similar to this require a lot of work?
    I have very little expertise in coding but I had been hoping to start my own blog
    soon. Anyway, if you have any recommendations or tips for new
    blog owners please share. I know this is off subject nevertheless
    I simply wanted to ask. Cheers!

  15. раскрутка сайта продвижение сайта https://prodvizhenie-sajtov15.ru/ .

  16. JerryMig表示:

    pharmacies in mexico that ship to usa mexican border pharmacies shipping to usa mexico drug stores pharmacies

  17. Do you mind if I quote a few of your articles as long as I provide credit and sources back to your blog?
    My blog is in the very same niche as yours and
    my users would certainly benefit from some of the information you provide here.
    Please let find sex near me know if this alright with
    you. Regards!

  18. сколько стоит аудит сайта prodvizhenie-sajtov15.ru .

  19. I do not even know how I ended up here, but I thought this
    post was great. I do not know who you are but definitely
    you’re going to a famous blogger if you are not already 😉 Cheers!

  20. My brother recommended I might like this web site. He used to be totally right.
    This post truly made my day. You can not consider simply how so much time I had spent for
    this information! Thank you!

  21. Louishycle表示:

    mexican mail order pharmacies best online pharmacies in mexico mexico drug stores pharmacies

  22. I absolutely love your blog.. Great colors & theme.
    Did you build this site yourself? Please reply back as I’m attempting to create my own personal website and would love to learn where you got this from or just what the theme is named.
    Kudos!

  23. TravisNOb表示:

    http://mexicanph.com/# mexican drugstore online
    pharmacies in mexico that ship to usa

  24. cunilingus表示:

    Howdy tһijs is kind of of ᧐ff topic but I was wondering if blogs use WYSIWYG
    ditors oг if yoս һave to manually code with HTML.
    I’m startijng a blog soon but have no copding experience so I
    wanted to get guidance from someone with experience. Any һelp ԝouⅼd be enormously appreсiated!

  25. JulioAxowl表示:

    medication from mexico pharmacy reputable mexican pharmacies online pharmacies in mexico that ship to usa

  26. JerryMig表示:

    medicine in mexico pharmacies pharmacies in mexico that ship to usa mexican pharmaceuticals online

  27. Interesting blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple tweeks
    would really make my blog jump out. Please let me know where you got your design.
    Appreciate it

  28. Hello, just wanted to say, I loved this blog post.

    It was inspiring. Keep on posting!

發佈留言

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