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,231,533 Responses

  1. I do not know whether it’s just me or if perhaps everybody else experiencing issues with your website.
    It seems like some of the written text in your posts are running off the screen. Can someone else please comment
    and let me know if this is happening to them as well?
    This might be a problem with my browser because I’ve had this happen before.

    Cheers

  2. It’s an amazing piece of writing in favor of all the web users; they will obtain benefit from it
    I am sure.

  3. Thanks in favor of sharing such a pleasant opinion, post
    is good, thats why i have read it completely

  4. Incredible points. Great arguments. Keep up the good effort.

  5. I think the admin of this web site is genuinely working hard in favor of his web site, as here every data
    is quality based stuff.

  6. Hello there, just became alert to your blog through Google, and found that it’s really informative.
    I’m going to watch out for brussels. I will appreciate if you continue this in future.
    Numerous people will be benefited from your writing.
    Cheers!

  7. Hello, I log on to your new stuff regularly. Your writing style is
    awesome, keep doing what you’re doing!

  8. Hi 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 guidance from someone
    with experience. Any help would be greatly appreciated!

  9. Hello! This is my first visit to your blog! We are a group of volunteers and
    starting a new project in a community in the
    same niche. Your blog provided us useful information to work on. You
    have done a outstanding job!

  10. It’s hard to come by experienced people on this topic, however, you sound like you know what you’re
    talking about! Thanks

  11. Since the admin of this web page is working,
    no uncertainty very quickly it will be well-known, due to its
    quality contents.

  12. I got this web site from my buddy who told me regarding this
    web page and at the moment this time I am browsing this web site and
    reading very informative content at this time.

  13. I do not even know how I finished up right here,
    but I believed this post was once good. I don’t recognize who you are however certainly you’re going to a famous blogger in case you aren’t already.
    Cheers!

  14. Hi terrific blog! Does running a blog similar to this take
    a massive amount work? I have no expertise in coding but I was hoping to start my own blog in the near future.
    Anyway, should you have any recommendations or tips for new blog owners please share.

    I know this is off subject however I simply needed to ask.
    Cheers!

  15. Howdy! Quick question that’s totally off topic. Do you know how to make your site
    mobile friendly? My web site looks weird when viewing from
    my iphone4. I’m trying to find a theme or plugin that might be able to fix this
    problem. If you have any recommendations,
    please share. Thanks!

  16. Thanks for the good writeup. It in reality was a amusement account it.
    Glance complicated to more added agreeable from you! By the way, how
    could we communicate?

  17. Your style is very unique in comparison to other
    people I’ve read stuff from. Thank you for posting when you’ve got the opportunity,
    Guess I’ll just book mark this site.

  18. Asking questions are really pleasant thing if you are not understanding something fully, but this
    post provides pleasant understanding even.

  19. It’s perfect time to make a few plans for the longer term and it
    is time to be happy. I have read this put up and if I may I desire to recommend
    you few fascinating things or suggestions. Maybe you could write next articles regarding this article.

    I desire to learn even more issues approximately it!

  20. You could definitely see your expertise within the work you
    write. The arena hopes for even more passionate writers
    like you who aren’t afraid to mention how they believe.
    All the time follow your heart.

  21. Howdy very cool website!! Guy .. Excellent ..
    Superb .. I’ll bookmark your site and take the feeds additionally?
    I am glad to seek out a lot of useful info here in the post,
    we’d like develop extra strategies on this regard, thanks
    for sharing. . . . . .

  22. Hey there! This is my first comment here so I just wanted to give a quick shout out and say I really enjoy reading through your blog posts.

    Can you suggest any other blogs/websites/forums that go over the same topics?
    Thanks a lot!

  23. It’s really a nice and helpful piece of info.
    I am glad that you simply shared this useful info with us.
    Please stay us up to date like this. Thank you for sharing.

  24. Appreciating the hard work you put into your blog and in depth information you offer.
    It’s great to come across a blog every once in a
    while that isn’t the same outdated rehashed material.
    Wonderful read! I’ve saved your site and I’m adding your RSS feeds to my
    Google account.

  25. Just want to say your article is as surprising.
    The clearness on your put up is just cool and i can think you are an expert
    in this subject. Fine with your permission let me to snatch your RSS
    feed to stay updated with drawing close post. Thanks
    a million and please carry on the enjoyable work.

  26. I like the valuable info you provide in your articles.
    I will bookmark your weblog and check again here
    frequently. I am quite certain I’ll learn plenty of new stuff right here!
    Best of luck for the next!

  27. Hi there! I know this is somewhat off topic but I was wondering if you knew
    where I could find a captcha plugin for my comment form?
    I’m using the same blog platform as yours
    and I’m having problems finding one? Thanks a lot!

  28. Useful info. Fortunate me I found your site by chance,
    and I’m surprised why this twist of fate didn’t
    happened earlier! I bookmarked it.

  29. Good day very cool site!! Guy .. Beautiful .. Wonderful
    .. I will bookmark your site and take the feeds additionally?
    I’m satisfied to seek out numerous helpful info right here within the submit, we need work out
    more strategies on this regard, thanks for sharing. . . . .
    .

  30. Mehrere Urlauber verlieren sich zwischen den verrammelten Strandbuden. Die, die
    regelmäßig zum Überwintern herkommen, erkennen die Insel nimmer
    wieder. Am Strand und im Hotel seien anni currentis mehr Spanier als sonst, berichtet ein Ehepaar aus Hannover – Deutsche dagegen punktuell.
    Und genau das bringt Mallorca nach neun Monaten Corona an den Rand des
    Kollapses. Unter Reisewarnungen und Quarantänepflicht hat zwar ganz Spanien zu leiden.
    Die Urlaubsregionen an der Nordküste und in Andalusien haben in den Sommermonaten aber auch einheimische Stammkundschaft – und die ist ihnen auch in diesem Sommer
    treu geblieben. Die Balearen dagegen leben fast komplett von Ausländern, für Spanier sind sie praktisch ein unerschlossen. Mit verheerenden Folgen: 13 Milliarden Euro Einnahmen sind den Inseln anni
    currentis entgangen, schätzt der Touristikverband. Beinahe 600.000 Menschen haben ihren Job verloren oder sind in Kurzarbeit.
    Viele deren haben kaum Reserven, sind finanziell auf null.

發佈留言

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