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,152,961 Responses

  1. 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 reply as I’m looking to create my own blog and would like to find out where u got
    this from. thank you

  2. Luca表示:

    wonderful issues altogether, you just received a new reader.
    What may you recommend about your publish that you simply made some days in the past?
    Any certain?

  3. bokep jepang表示:

    Hello, i think that i saw you visited my blog so i came to “return the favor”.I’m trying to find things to enhance my
    web site!I suppose its ok to use a few of your ideas!!

  4. link bokep表示:

    of course like your web-site but you need to
    test the spelling on quite a few of your posts.
    A number of them are rife with spelling issues and I find it very
    troublesome to inform the truth nevertheless I will definitely come back
    again.

  5. You’ll Never Guess This Anxiety Treatment Natural’s Tricks anxiety treatment natural

  6. Five Killer Quora Answers On Replacement Double Glazing Units Near
    Me replacement double Glazing units near me

  7. baskili poset表示:

    You actually make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand.
    It seems too complex and extremely broad for me. I’m looking forward
    for your next post, I will try to get the hang of it!

  8. The 10 Scariest Things About Top Single Strollers Top Single strollers

  9. Noella表示:

    When I initially left a comment I seem to have clicked on the
    -Notify me when new comments are added- checkbox and now each time a comment is added I receive
    4 emails with the same comment. Perhaps there is a way you
    can remove me from that service? Appreciate it!

  10. 13 Things You Should Know About Slot Sites
    That You Might Not Know Real Money Slots (Dencker-Burnham.Mdwrite.Net)

  11. Hеllo there I am so thrilled I found your website, I гeally found you by mistakе,
    while I was Ƅrօwsing on Yahoo for somethіng else, Nonetheless
    I am here now and would just like to say many thanks for a
    marvelous poѕt and a all round exciting blog (I also love the theme/design), I don’t have
    time to read it all at the minute but I have
    bookmarked it and also added your RSS feeds, so when I have time
    Ι will be back to гead more, Please do keeρ up the superb work.

    Also visit my homepage … Dedicatedproxies.Net

  12. A Look At The Future What Is The Glass Door Repair Cambridge Industry Look Like In 10 Years?
    Timber Double Glazed Windows Cambridge (http://Cse.Google.Co.Kr/Url?Sa=T&Url=Http://Www.Diggerslist.Com/6652688Cdadba/About)

  13. Pills information sheet. Short-Term Effects.
    buy generic inderal without prescription
    Best information about medicines. Read now.

  14. Www.Bevnp.Com表示:

    The 10 Most Scariest Things About High Wycombe Windows And Doors Aluminium Doors
    High Wycombe; http://Www.Bevnp.Com,

  15. Trusted Slots表示:

    Why Do So Many People Want To Know About Winning Slots? Trusted Slots

  16. Chevrolet 993表示:

    Komentari su kao retrovizor – omogucavaju pogled unazad na razlicite stavove.
    Chevrolet Vega je bio inovativni model iz 70-ih.
    Auto mi je toliko star da mu treba vlastiti istorijski spomenik.

  17. What’s The Job Market For Online Shopping Uk For Clothes Professionals Like?
    online shopping uk for clothes

  18. Hello there! Would you mind if I share your blog with my facebook group?
    There’s a lot of folks that I think would really enjoy your content.

    Please let me know. Many thanks

  19. New customers also have the alternative for this weekend with a welcome present
    of $10 in Bonus Bets with any $20 bet on on NFL Sunday games November 5.

    My web page :: 메이저사이트

  20. Благодарю ! Заходите и еще на собственный сервис 😉 Застекления балкона под ключ

  21. ai image表示:

    Thanks for your personal marvelous posting! I quite enjoyed reading it,
    you happen to be a great author.I will be sure to bookmark your blog
    and will eventually come back at some point. I want to encourage you to ultimately continue your great writing, have a nice weekend!

  22. 188bet18050表示:

    Does your site have a contact page? I’m having a tough time locating
    it but, I’d like to send you an e-mail. I’ve got some
    creative ideas for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing
    it expand over time.

  23. Alecia表示:

    On-line media and marketing.

    Also visit my web-site cosori air fryer – Alecia

  24. 3 Ways That The Window Repair Near Can Affect Your
    Life Upvc Window repairs

發佈留言

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