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...

831,040 Responses

  1. I’m not sure why but this site is loading extremely
    slow for me. Is anyone else having this issue or is
    it a issue on my end? I’ll check back later and see if the problem still exists.

  2. Do you mind if I quote a few of your articles as long as I provide credit and sources back to your blog?
    My website is in the exact same niche as yours and my
    visitors would definitely benefit from some of
    the information you present here. Please let me know if this okay with you.
    Thanks!

  3. I blog often and I genuinely thank you for your
    information. This article has really peaked my interest.
    I am going to bookmark your site and keep checking for new information about once per week.
    I subscribed to your Feed too.

  4. I go to see daily a few web sites and sites to read
    articles, but this weblog presents quality based writing.

  5. For most recent news you have to pay a quick visit web and
    on the web I found this web page as a most excellent website for hottest updates.

  6. We are a group of volunteers and starting a new scheme
    in our community. Your website provided us with valuable info
    to work on. You have done an impressive job and our whole community
    will be grateful to you.

  7. I love what you guys are up too. Such clever work and exposure!
    Keep up the terrific works guys I’ve included you guys to my own blogroll.

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

    Outstanding Blog!

  9. I am in fact pleased to glance at this weblog
    posts which includes plenty of helpful facts, thanks for providing these data.

  10. Write more, thats all I have to say. Literally, it seems as though
    you relied on the video to make your point. You clearly know what youre talking about, why waste your intelligence on just posting videos to your blog when you could be giving us something
    enlightening to read?

  11. Just wish to say your article is as amazing. The clarity in your post is just great and i could assume you
    are an expert on this subject. Fine with your permission allow me to grab your feed to keep updated
    with forthcoming post. Thanks a million and please carry on the rewarding work.

  12. Peculiar article, totally what I was looking for.

  13. Having read this I believed it was very informative.
    I appreciate you finding the time and energy to put this informative article together.
    I once again find myself personally spending a significant amount of time both reading and posting comments.
    But so what, it was still worthwhile!

  14. It’s actually a great and useful piece of info. I am happy that you shared this
    helpful info with us. Please stay us up to date like this.
    Thank you for sharing.

  15. I have been surfing online more than three hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. Personally, if all website owners and bloggers made good content as you
    did, the net will be much more useful than ever before.

  16. Generally I don’t learn post on blogs, but I wish to say that this write-up very forced me to take a look at and do so!
    Your writing taste has been amazed me. Thank you, very great
    post.

  17. Esteban表示:

    Howdy! I could have sworn I’ve been to your blog before but after
    looking at many of the posts I realized it’s new to me.

    Regardless, I’m definitely happy I came across it and I’ll be bookmarking it and checking back frequently!

  18. I have to thank you for the efforts you’ve put in penning this website.
    I really hope to check out the same high-grade content from you later on as well.
    In truth, your creative writing abilities has encouraged me to get my own, personal site now 😉

  19. Hey great website! Does running a blog such as this require a massive
    amount work? I have no expertise in computer programming however
    I had been hoping to start my own blog soon. Anyway, if
    you have any ideas or tips for new blog owners please share.
    I know this is off topic nevertheless I simply had to ask.
    Thanks a lot!

  20. I am truly grateful to the owner of this web site who has
    shared this impressive post at at this time.

  21. It’s really a cool and helpful piece of info. I am
    happy that you simply shared this helpful information with
    us. Please keep us informed like this. Thanks for sharing.

  22. Hello my family member! I want to say that this article is awesome, great written and include approximately all significant infos.
    I would like to see extra posts like this .

  23. Howdy! I simply would like to give you a huge thumbs up for the excellent information you’ve got right here on this post.

    I’ll be coming back to your blog for more soon.

  24. damenuhren表示:

    Schon seit Anbeginn der Rolex-Ära legte Gründer und Visionär Hans Wilsdorf gleichermaßen Wert auf die gehobenen Ansprüche von Männern und Frauen an eine hochwertige Uhr.
    Mit der Rolex Datejust entstand nicht nur ein Meisterwerk an Perfektion – als erster
    Chronometer mit Kalenderfunktion war die Rolex Datejust zudem typisch Meilenstein in der Geschichte von Rolex.
    Neben anderen das Rolex Lady Datejust entwickelte die
    Luxusmanufaktur Ende der 1950er-Jahre das weibliche Pendant zu diesem modernen Klassiker der Uhrmacherkunst.

    In Zeiten von Pionierinnen und Abenteurerinnen war
    eine Uhr wie die Rolex Lady Datejust nicht nur schmückendes Accessoire, sondern sollte auch durch Funktionalität in Alltag und Beruf überzeugen. Sie
    gilt heute wie in unseren Studententagen als Symbol für die einzigartigen Stärken einer Frau, die
    von selbstbewussten Markenbotschafterinnen aus Kultur, Sport und Wissenschaft
    verkörpert werden. Jede Frau war und ist mit dieser Uhr bisherig im wahrsten sinne des wortes
    „up to date”. Wir bei CHRONEXT legen höchsten Wert auf die Qualität unserer Luxusuhren.

  25. I always spent my half an hour to read this webpage’s articles or reviews daily along
    with a cup of coffee.

  26. Thank you for the auspicious writeup. It in reality was once a
    enjoyment account it. Look advanced to more brought agreeable from you!

    By the way, how can we be in contact?

  27. 과유불급表示:

    I like what you guys tend to be up too. This kind of clever work and exposure!
    Keep up the wonderful works guys I’ve you guys to my own blogroll.

  28. If some one wishes expert view about blogging afterward i suggest him/her
    to go to see this weblog, Keep up the fastidious work.

  29. It’s nearly impossible to find educated people on this subject, however,
    you seem like you know what you’re talking about! Thanks

發佈留言

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