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,227,708 Responses

  1. I’m now not sure the place you’re getting
    your information, but good topic. I needs to spend a while learning much more or working out
    more. Thanks for excellent information I used to be in search of this info for my mission.

  2. Howdy! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m undoubtedly enjoying your blog and look forward to new posts.

  3. Nice respond in return of this query with solid
    arguments and describing everything on the topic of that.

  4. Superb blog you have here but I was curious
    about if you knew of any message boards that cover the
    same topics talked about here? I’d really like to be a part of group where I can get feedback from other experienced people that share the same interest.
    If you have any suggestions, please let me know. Thanks a lot!

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

  6. Hi to every one, it’s genuinely a good for me to go to see this site, it
    includes precious Information.

  7. This is a great tip especially to those fresh to the blogosphere.
    Short but very precise information… Many thanks for sharing this one.

    A must read article!

  8. Its such as you read my mind! You appear to grasp so much about this,
    such as you wrote the e-book in it or something. I feel that
    you can do with some p.c. to power the message house a bit, however instead of
    that, that is wonderful blog. An excellent read.
    I’ll definitely be back.

  9. Hi there, I enjoy reading all of your article. I like to write a little comment
    to support you.

  10. Greetings! This is my first comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading your posts.

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

  11. Oh my goodness! Amazing article dude! Thank you, However I
    am going through issues with your RSS. I don’t know
    why I am unable to join it. Is there anybody else having the same
    RSS problems? Anyone who knows the answer will you kindly respond?
    Thanx!!

  12. I absolutely love your blog.. Very nice colors & theme.
    Did you build this web site yourself? Please reply back as I’m
    attempting to create my own personal site and would
    love to know where you got this from or just what the theme is named.
    Thanks!

  13. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way,
    how could we communicate?

  14. excellent put up, very informative. I wonder why the opposite experts of
    this sector do not notice this. You should proceed your writing.
    I’m confident, you have a great readers’ base already!

  15. Howdy, i read your blog from time to time and i own a similar one
    and i was just curious if you get a lot of spam remarks? If so how do you
    protect against it, any plugin or anything you can advise?
    I get so much lately it’s driving me mad so any help
    is very much appreciated.

  16. Nice post. I learn something new and challenging on blogs I stumbleupon every day.
    It will always be exciting to read articles from other authors and use
    something from other websites.

  17. We’re a bunch of volunteers and starting a new scheme in our community.
    Your website provided us with valuable information to work on. You’ve performed a formidable process and our whole group will be grateful to you.

  18. Hi my family member! I want to say that this
    article is awesome, nice written and come with approximately all significant infos.
    I would like to look more posts like this .

  19. Thanks a lot for sharing this with all people you really
    recognise what you are talking about! Bookmarked.
    Please additionally consult with my website =). We could have a hyperlink alternate agreement between us

  20. Hiya very nice site!! Guy .. Beautiful .. Amazing ..
    I’ll bookmark your blog and take the feeds also?
    I am happy to find so many useful info here within the put up, we need develop extra strategies on this regard,
    thank you for sharing. . . . . .

  21. Hello! Someone in my Facebook group shared this
    site with us so I came to take a look. I’m definitely enjoying the information. I’m book-marking
    and will be tweeting this to my followers!
    Fantastic blog and terrific style and design.

  22. Jeannie表示:

    Very energetic blog, I loved that bit. Will there be
    a part 2?

  23. Attractive section of content. I just stumbled upon your site
    and in accession capital to say that I get in fact enjoyed account your weblog posts.
    Anyway I will be subscribing in your feeds and even I fulfillment you get right of entry to persistently quickly.

  24. Hi there just wanted to give you a quick heads up.

    The words in your article seem to be running off the screen in Internet explorer.
    I’m not sure if this is a formatting issue or something to do with
    internet browser compatibility but I figured I’d post to let you
    know. The style and design look great though! Hope you get
    the issue resolved soon. Many thanks

  25. I’ve been browsing online more than 4 hours today,
    yet I never found any interesting article like yours. It’s pretty worth
    enough for me. In my view, if all web owners and
    bloggers made good content as you did, the net will be much more useful than ever before.

  26. Great delivery. Outstanding arguments. Keep up the amazing work.

  27. Oh my goodness! Awesome article dude! Thank you so much, However
    I am encountering problems with your RSS. I don’t understand why I
    can’t join it. Is there anybody else having the same RSS issues?
    Anyone that knows the solution can you kindly respond?
    Thanks!!

  28. Socket Secure Layer یا گواهی SSL یکی از
    مفیدترین راه حل های دیجیتالی برای احراز هویت وب سایت است
    که توسط Netscape در سال 1995 معرفی شد.

    این فقط به این معنی است که SSL یک پروتکل اینترنتی برای ایمن سازی انتقال داده ها از مرورگر شخصی
    است که وب سایت شما را به سرور مشاهده می کند.

    بنابراین، یک فرآیند رمزگذاری ایمن را تکمیل می کند و هیچ کس دیگری نمی تواند ببیند چه اتفاقی در حال
    رخ دادن است. داده های منتقل شده از طریق
    پروتکل HTTP یک اتصال اینترنتی
    امن نیست. SSL آن را به HTTPS تبدیل می کند و فرآیند انتقال داده را ایمن می کند.

    امروزه اکثر مرورگرها از این پروتکل پشتیبانی می کنند
    و برخی به شما اجازه نمی دهند که از طریق HTTP یک اتصال ناامن ایجاد کنید.

    علاوه بر این، استفاده از SSL
    یکی از بهترین راه ها برای ایمن سازی
    پروتکل های FTP، IMAP و SMTP است.

  29. Great goods from you, man. I’ve understand your stuff previous to and you’re just too
    great. I really like what you’ve acquired here, certainly like what you’re stating and the way in which you say it.
    You make it enjoyable and you still take care of
    to keep it wise. I cant wait to read much more from you.
    This is actually a wonderful website.

  30. Hello everyone, it’s my first visit at this web page,
    and piece of writing is genuinely fruitful in support of me, keep
    up posting such articles or reviews.

發佈留言

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