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,602,914 Responses

  1. royal cane表示:

    Hello! I could have sworn I’ve been to this site before but
    after browsing through some of the post I realized it’s new to me.
    Anyways, I’m definitely glad I found it and I’ll be bookmarking
    and checking back frequently!

  2. xxxlinks.top表示:

    I was recommended this web site by my cousin. I’m not sure whether this post is written by
    him as nobody else know such detailed about my difficulty.

    You are incredible! Thanks!

  3. Hi all, here every one is sharing these kinds of know-how, so it’s fastidious to read this blog, and I used to go to
    see this webpage every day.

  4. Sus 500表示:

    Hey there! I just wanted to ask if you ever have any trouble
    with hackers? My last blog (wordpress) was hacked and
    I ended up losing a few months of hard work due to no backup.
    Do you have any methods to protect against hackers?

  5. Jamesavata表示:

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

  6. Jamesavata表示:

    пин ап вход – пин ап казино

  7. It is a well-known fact that assignments and homework tasks allocated by professors contribute significantly to final grades. Are you working hard on your thermodynamics assessment jobs to enhance your overall grades? However, still not getting the desired result? We very well know how you are feeling. Well, no need to be anxious anymore. The professional tutors of Thermodynamics Assignment Help reference guide will always be by your side to alleviate all your stress. Approach right now and be the topper of your class!

  8. Jamesavata表示:

    пин ап казино официальный сайт – пин ап казино официальный сайт

  9. JosephOxipt表示:

    pet pharmacy online Find drug information. Find pill info. xanax online pharmacy

  10. Medication information leaflet. Long-Term Effects.
    cost of cheap nortriptyline without a prescription
    Some about meds. Read here.

  11. Josephren表示:

    Entering the Melbet promo code on the official website of the offshore bookmaker Melbet gives the bettor the opportunity to receive additional funds to the deposit game account in the personal account, free bets and some other gifts from the administration of the bookmaker company https://globalmarine.ru/news/promokod_melbet_bonus_do_15_000_rub.html

  12. Appreciating the time and effort you put into
    your blog and detailed information you present.

    It’s nice to come across a blog every once in a while that isn’t
    the same out of date rehashed information. Excellent read!
    I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

  13. Woah! I’m really loving the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s very hard to
    get that “perfect balance” between user friendliness and visual appearance.
    I must say you’ve done a superb job with this. Additionally, the blog loads extremely fast for me on Chrome.
    Exceptional Blog!

  14. Vknigah.com表示:

    Hello, i think that i saw you visited my website so i came to “return the favor”.I am
    attempting to find things to improve my web site!I suppose its ok to use some of your ideas!! https://vknigah.com/user/JacquelynW46/

  15. Jamesavata表示:

    пин ап казино зеркало – пин ап казино официальный сайт

  16. Williamuniot表示:

    пин ап зеркало – pinup 2025

  17. JesseDex表示:

    пин ап казино: https://pinupkz.life/

  18. JesseDex表示:

    пин ап зеркало: https://pinupkz.life/

  19. Jamesavata表示:

    пин ап – пин ап вход

  20. koitoto表示:

    Hello there, just became aware of your blog through Google, and found that it is truly informative.

    I am gonna watch out for brussels. I will appreciate if you continue this in future.
    A lot of people will be benefited from your writing.
    Cheers!

  21. I am really enjoying the theme/design of your website. Do you ever run into any web
    browser compatibility problems? A couple of my blog readers have complained
    about my site not operating correctly in Explorer but looks
    great in Firefox. Do you have any tips to help fix this problem?

  22. pornhome.top表示:

    Aw, this was an incredibly nice post. Taking a few minutes and actual effort to
    generate a superb article… but what can I say… I put things off a whole lot and don’t manage to get nearly anything done.

  23. Jamesavata表示:

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

  24. Hi to all, the contents existing at this site are
    in fact amazing for people knowledge, well, keep up the good work fellows.

  25. You can definitely see your expertise within the article you write.
    The sector hopes for even more passionate writers such as you who aren’t afraid to mention how they believe.
    All the time go after your heart.

  26. Great article! That is the type of info that are supposed to be shared
    around the internet. Shame on Google for now not positioning this publish upper!
    Come on over and seek advice from my web site . Thank you =)

發佈留言

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