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

  1. Hello there! I could have sworn I’ve visited this site before but after
    looking at a few of the posts I realized it’s new to me.

    Anyways, I’m certainly happy I stumbled upon it and
    I’ll be bookmarking it and checking back often!

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

  3. Profile表示:

    Hello this is somewhat of off topic but I was wanting to know
    if blogs use WYSIWYG editors or if you have to manually
    code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to
    get advice from someone with experience.
    Any help would be greatly appreciated!

  4. Lori表示:

    %%

    Visit my web blog :: seat car keys – Lori,

  5. How to file an asbestos claim [https://botdb.win]

  6. sitemap.xml表示:

    It’s wonderful that yoou aare getting thoughts frdom this article aas
    well as from our discusskon madee aat this place.

  7. sitemap表示:

    Thanks for snaring your thoughtts onn 5042. Regards

  8. My brother suggested I would possibly like this web site.
    He was entirely right. This put up truly made my day. You cann’t
    consider simply how so much time I had spent for this information! Thank
    you!

  9. I quite like reading through an article that will make men and women think.
    Also, thanks for permitting me to comment!

  10. After exploring a number of the blog posts on your blog, I seriously
    appreciate your way of writing a blog. I saved as a favorite it to my bookmark website list
    and will be checking back in the near future. Take a
    look at my web site as well and tell me what you
    think.

  11. More info表示:

    It’s actually a great and helpful piece of information. I am happy that you shared this helpful
    information with us. Please keep us up to date like this.

    Thank you for sharing.

  12. Source URL表示:

    I every time used to read article in news papers but now as I am a user of
    net therefore from now I am using net for articles, thanks to web.

  13. It’s really a cool and helpful piece of info. I’m glad that you just shared this helpful information with us.
    Please keep us informed like this. Thanks for sharing.

  14. sitemap表示:

    What’s uup colleagues, how is the whooe thing, annd what yyou want to sayy abot this puece oof writing, inn myy vjew itss really awesome
    designedd for me.

  15. Hi, I would like to subscribe for this website to take latest updates, so where
    can i do it please assist.

  16. Archer表示:

    asbestos claims (Archer)

  17. Website表示:

    Way cool! Some extremely valid points! I appreciate you penning this post and also the rest of the website is also really good.

  18. Jorja表示:

    %%

    My blog post; double glazed windows lambeth (Jorja)

  19. diagnosis表示:

    %%

    Here is my site … diagnosis

  20. Thanks in support of sharing such a pleasant opinion, article is
    fastidious, thats why i have read it completely

  21. Wow, amazing blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is wonderful, let alone the content!

  22. Roxanna表示:

    asbestos lawsuit (Roxanna)

  23. Profile表示:

    These are really impressive ideas in concerning blogging.

    You have touched some fastidious points here. Any
    way keep up wrinting.

發佈留言

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