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,247,280 Responses

  1. Lovely just what I was looking for. Thanks to the author
    for taking his time on this one.

  2. Raymond表示:

    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?

  3. What’s up to all, the contents existing at this website are in fact
    remarkable for people experience, well, keep up the nice work
    fellows.

  4. certainly like your website but you have to take a
    look at the spelling on quite a few of your posts. Several of them
    are rife with spelling problems and I to find it very bothersome to tell the
    truth on the other hand I will surely come back again.

  5. Thank you for some other fantastic post. Where else may just anyone get
    that type of info in such a perfect approach of writing?
    I have a presentation subsequent week, and I am on the search for such information.

  6. It’s a pity you don’t have a donate button! I’d definitely donate to this
    brilliant blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to new updates and will share this website with my Facebook
    group. Chat soon!

  7. bonrollen表示:

    Wie unterscheiden sich Thermorollen von Normalpapier Kassenrollen und wo werden diese eingesetzt?
    Bei Bonrollen aus Thermopapier ist eine Seite des Papier thermoaktiv beschichtet, dass heißt es verfärbt sich unter der Einwirkung von Hitze.

    Bei Bonrollen aus Normalpapier ist das Papier nicht beschichtet,
    der Druck erfolgt für gewöhnlich über einen Nadeldrucker, der widerum ein Farbband benötigt.
    Bei einem Thermodrucker erzeugt der Druckkopf diese Hitze an den zu bedruckenden Stellen, so, dass Schrift oder Grafiken emergieren. Aufgrund der
    Hitzeempfindlichkeit von Thermopapier ist es überall dort ungeeignet, wo es außerhalb des Druckers Hitze ausgesetzt wird, z.B.

    Rand eines vorgewärmten Tellers in der Küche zu legen. Was kann ich
    unter Kassenrollen mit Lastschrifttext verstehen? Diesen unterschreibt Ihr
    Kunde bei der Bezahlung mit seiner EC-Karte. Bei Bonrollen mit Lastschrifttext
    befindet sich auf der Rückseite des Papier ein allgemeingültiger
    Lastschrifttext. Sofern Ihre Kunden stets eine PIN zur Authorisierung eingeben müssen, benötigen Sie keine Rollen mit Lastschrifttext.
    In der Gesamtheit sind diese Rollen 57mm breit und aus Thermopapier.

  8. Magnificent beat ! I would like to apprentice at the same time as you amend your website,
    how could i subscribe for a blog website? The account helped me a
    acceptable deal. I were a little bit familiar of this your broadcast provided brilliant transparent idea

  9. weixing13.com表示:

    Have you ever considered writing an e-book or guest authoring on other websites?
    I have a blog centered on the same topics you discuss and would really like to have you share some stories/information.
    I know my audience would enjoy your work. If you’re even remotely interested, feel free to shoot me an e mail.

  10. Hi friends, its fantastic paragraph concerning tutoringand fully defined, keep it up all the time.

  11. Rattling superb visual appeal on this web site, I’d value it 10.

  12. Currently it appears like Movable Type is the best blogging platform out there right now.
    (from what I’ve read) Is that what you are using on your blog?

  13. Hi there, 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 responses?
    If so how do you stop it, any plugin or anything you can suggest?

    I get so much lately it’s driving me insane so any assistance
    is very much appreciated.

  14. stimulating review. Moreover visit our site to obtain situs slot online.
    Now this incredible web site has received realization right from slot
    deposit pulsa betting those of you in Dalam negri. Generally there will
    be a lot of stunning promos climate join now.

  15. grandong表示:

    What’s up, just wanted to say, I loved this post.
    It was helpful. Keep on posting!

  16. Hello! I’m at work surfing around your blog from my new iphone 4!

    Just wanted to say I love reading through your blog and look
    forward to all your posts! Keep up the excellent work!

  17. Também possuo um blog de nicho semelhante ao seu e seu site me serve de inspiração continue com este ótimo trabalho.
    parabéns .

  18. betot.ru表示:

    Its like you learn my mind! You seem to know so much about this,
    such as you wrote the e-book in it or something. I think that you just could
    do with some p.c. to pressure the message house a
    bit, however instead of that, this is great blog. A great read.
    I will certainly be back.

  19. I like looking through an article that can make men and
    women think. Also, many thanks for permitting me to comment!

  20. Unquestionably consider that which you stated. Your favourite justification seemed to
    be at the net the simplest thing to be aware of.

    I say to you, I definitely get irked while folks think about worries that they just don’t recognise about.
    You controlled to hit the nail upon the highest as smartly as defined out the
    whole thing without having side effect , people could take a signal.
    Will likely be again to get more. Thank you

  21. I feel this is one of the such a lot important info for me.
    And i’m satisfied studying your article. However wanna
    remark on some common issues, The site taste is great, the articles is actually nice : D.
    Just right task, cheers

  22. anotepad.com表示:

    Aw, this was an extremely good post. Taking the time and actual
    effort to produce a good article… but what can I say… I put things off a lot and don’t
    manage to get nearly anything done.

  23. We will teach you how to earn $ 7000 per hour.

    Why? We will profit from your profit.https://go.binaryoption.ae/FmUKhe

  24. Hello very nice web site!! Man .. Beautiful ..

    Superb .. I’ll bookmark your site and take the feeds additionally?
    I’m glad to search out numerous useful information right here in the post, we’d like develop more techniques in this
    regard, thanks for sharing. . . . . .

  25. Hi colleagues, how is the whole thing, and what you wish for
    to say about this article, in my view its actually amazing in favor of me.

  26. Karen表示:

    Hello colleagues, nice post and good arguments commented at this place, I am genuinely enjoying
    by these.

  27. Hmm it seems like your blog ate my first comment (it was
    extremely long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying
    your blog. I as well am an aspiring blog blogger but I’m still new to the whole thing.
    Do you have any tips and hints for newbie blog writers? I’d really
    appreciate it.

  28. No matter if some one searches for his essential thing,
    so he/she wants to be available that in detail, thus that thing is maintained over here.

  29. Undeniably believe that which you stated.
    Your favorite reason seemed to be on the internet the simplest thing to
    be aware of. I say to you, I definitely get irked while
    people think about worries that they plainly
    don’t know about. You managed to hit the nail upon the top and also defined out the whole thing without having
    side-effects , people could take a signal. Will likely be back to get more.
    Thanks

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

發佈留言

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