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

908,054 Responses

  1. Wonderful work! This is the type of info that are
    supposed to be shared across the internet. Shame on Google for no longer positioning this submit higher!

    Come on over and seek advice from my website .

    Thank you =)

  2. It’s going to be end of mine day, except before ending
    I am reading this enormous piece of writing to increase my experience.

  3. Kennith表示:

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

  4. Hello i am kavin, its my first time to commenting anyplace,
    when i read this post i thought i could also make comment
    due to this sensible article.

  5. Excellent goods from you, man. I have understand your stuff previous to and you’re just
    extremely fantastic. I really like what you have acquired here, really like
    what you’re stating and the way in which
    you say it. You make it entertaining and you still take care of to keep
    it sensible. I cant wait to read much more from you.
    This is actually a terrific website.

  6. Undeniably believe that that you stated. Your favourite reason appeared
    to be at the net the simplest factor to take into accout of.
    I say to you, I definitely get irked whilst other people think about concerns
    that they just don’t recognize about. You controlled to hit
    the nail upon the highest and defined out the entire thing without having side-effects , folks can take a
    signal. Will probably be back to get more. Thank you

  7. I believe this is among the so much significant information for me.
    And i am satisfied studying your article. But
    should remark on some general things, The website taste is ideal, the articles
    is really nice : D. Good process, cheers

  8. Hello to every single one, it’s actually a pleasant for me to pay
    a quick visit this website, it includes precious Information.

  9. Good day! I know this is kinda off topic however ,
    I’d figured I’d ask. Would you be interested in exchanging
    links or maybe guest writing a blog article or vice-versa?
    My website discusses a lot of the same subjects as yours and I think we could greatly benefit from each other.
    If you might be interested feel free to send me an e-mail.

    I look forward to hearing from you! Excellent blog by the way!

  10. Du suchst eine Herrenuhr, die Du im Büro tragen kannst.
    Eine Uhr, die im Kundengespräch Deine distinguiert elegante Erscheinung unterstreicht, die Schmuck und funktionaler Zeitanzeiger ist.
    Eine Uhr, die Seriosität und Professionalität ausstrahlt, Vertrauen aufbaut und trotzdem nicht von Deiner Kompetenz ablenkt.

    Lederarmband oder Metallarmband, was passt da besser?
    Holzarmbanduhr oder Schweizer Chronograph? Seiko,
    Citizen et al. namhafte Marken haben garantiert die passende Uhr für Dich im Sortiment.
    Aber was, wenn Du zum Sport gehst? Für den Sport brauchst Du eine andere Uhr.
    Abhängig davon, ob Du segelst, tauchst oder läufst, ob Du schwimmen gehst
    oder Outdoor-Sport einer anderen Art betreibst, sollte
    Deine Armbanduhr bestimmte Funktionen erfüllen. Die elegante Armbanduhr, die
    Du vormittags im Büro trägst, ist vermutlich kaum wasserdicht und kann auch nicht weitaus mehr die Zeit anzuzeigen. Hier erfährst Du, worauf
    Du beim Kauf Deiner Uhr achten solltest. Die Uhr als einziger erlaubter Schmuck
    des Mannes? Die Uhr als einziger erlaubter Schmuck des Mannes?
    Immer wieder raten Karriereratgeber dazu, dass Männer keinen Schmuck tragen sollten, dass sie womöglich auch auf die Armbanduhr verzichten sollten.

  11. Great blog here! Also your web site loads up fast! What host
    are you using? Can I get your affiliate link to your host?
    I wish my site loaded up as quickly as yours lol

  12. Elana表示:

    Great blog! Do you have any hints for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.
    Would you recommend starting with a free platform
    like WordPress or go for a paid option? There are so many choices out there that
    I’m totally overwhelmed .. Any ideas? Appreciate it!

  13. Awesome! Its really amazing post, I have got much clear idea regarding from this piece of
    writing.

  14. Spot on with this write-up, I seriously think this web site needs much more attention. I’ll probably be returning to read more, thanks for the advice!

  15. Nice post. I was checking continuously this weblog and I’m inspired!

    Extremely helpful info specifically the closing phase 🙂 I deal with such information much.
    I used to be seeking this particular information for a long
    time. Thank you and good luck.

  16. I got this website from my buddy who informed me on the topic of
    this web site and now this time I am visiting this website and reading very informative posts at this place.

  17. We stumbled over here coming from a different website and thought I may as well check things out.
    I like what I see so now i’m following you. Look forward to looking over your web page
    yet again.

  18. Hello, its nice piece of writing on the topic of media print, we all understand media is a great source of data.

  19. I blog quite often and I really thank you for your information. This article has really peaked my
    interest. I will bookmark your blog and keep checking for new information about once a week.
    I opted in for your RSS feed too.

  20. Excellent blog you’ve got here.. It’s difficult to find good quality
    writing like yours nowadays. I truly appreciate people like you!
    Take care!!

  21. Wow, superb blog structure! How long have you ever been running a blog for?
    you make running a blog glance easy. The full look
    of your web site is wonderful, let alone the content!

  22. I was recommended this web site by way of my cousin. I am not sure whether or not this submit is written by him as
    nobody else know such designated approximately my problem.
    You are amazing! Thanks!

  23. Hi, i think that i saw you visited my website 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!!

  24. Hey, I think your website might be having browser compatibility issues.
    When I look at your blog in Ie, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that,
    wonderful blog!

  25. cqms.skku.edu表示:

    I will immediately grab your rss as I can not find your email subscription link or e-newsletter service.
    Do you’ve any? Please allow me understand in order that I may just subscribe.
    Thanks.

  26. Link exchange is nothing else however it is simply placing the other
    person’s website link on your page at appropriate place and other person will
    also do same in favor of you.

  27. Hey there! Do you know if they make any plugins to help with SEO?

    I’m trying to get my blog to rank for some targeted keywords but
    I’m not seeing very good results. If you know
    of any please share. Thanks!

  28. I was recommended this blog through my cousin. I
    am now not certain whether this submit is written through him as no one
    else realize such special approximately my problem.
    You are incredible! Thanks!

  29. Hello to all, how is the whole thing, I think every one is getting more from this web
    site, and your views are fastidious designed for new viewers.

  30. Heya fantastic blog! Does running a blog such as this require a lot of work?
    I’ve virtually no understanding of programming but I was hoping to start my own blog soon. Anyway, if you have
    any ideas or techniques for new blog owners please share.
    I know this is off subject however I simply needed to ask.
    Many thanks!

發佈留言

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