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,248,998 Responses

  1. Your style is very unique compared to other folks I have
    read stuff from. Thank you for posting when you’ve got the opportunity, Guess I’ll just bookmark this page.

  2. Vasseria表示:

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

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

  4. Hi there, just became aware of your blog through Google, and found that it
    is really informative. I am gonna watch out for brussels.
    I’ll be grateful if you continue this in future. Lots of people will
    be benefited from your writing. Cheers!

  5. Hi! This post could not be written any better! Reading this post reminds me of my
    previous room mate! He always kept talking about this.
    I will forward this write-up to him. Pretty sure
    he will have a good read. Thanks for sharing!

  6. Very good info. Lucky me I ran across your site by chance (stumbleupon).
    I have saved as a favorite for later!

  7. Hi there! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to
    no backup. Do you have any solutions to stop hackers?

  8. pbase.com表示:

    Thank you for the auspicious writeup. It in fact
    was a amusement account it. Look advanced to more added
    agreeable from you! However, how could we communicate?

  9. Awesome issues here. I’m very satisfied to look your post.
    Thank you so much and I’m taking a look forward to touch you.
    Will you please drop me a mail?

  10. Salon RTB表示:

    Appreciate this post. Will try it out.

    Feel free to visit my web site: Salon RTB

  11. saugroboter表示:

    Zudem sei die Akkureichweite mit nur 83 Minuten deutlich schlechter und der Preis
    hoch. Immerhin durch seine starke Saugleistung, die vielen Sensoren und die Qualität konnte der Marken Saugroboter punkten. Da Vorwerk Kobold VR300 direkt vom Hersteller vertrieben wird, liegen uns dazu keine Kundenbewertungen unabhängiger
    Onlineshops vor. Vorwerk Kobold VR300 Saugroboter erhalten Sie exklusiv im Vorwerk-Onlineshop.
    Was ist ein Saugroboter? In der Überzahl Modelle sind flach genug gebaut,
    um auch unter niedrige Möbel saugen zu können. Produziert werden die Saugroboter
    europäisch a fortiori von bekannten Haushaltsgeräteherstellern, wie Vorwerk oder Dyson. Staubsaugerroboter sind intelligente, meist runde Haushaltsgeräte mit einem Durchmesser
    von durchschnittlich 30 Zentimetern. In den USA, England und Asien kommen die Geräte dagegen eher von Herstellern, die im Innern der Robotertechnologie zuhause sind, wie
    die iRobot Modelle oder Saugroboter von Xiaomi bzw.

    Roborock. Letztere eignen sich extra für Nutzer ohne
    große Wohnung, da ihr Zick-Zack-Modus auf großen Flächen sehr viel Energie verbraucht.
    Im Handel sind Staubsaugerroboter mit Cloud-Anbindung und intelligenter Laser Navigation, mit Kamera und AI
    oder ohne WLAN mit Zufallsnavigation erhältlich. Wie funktioniert ein Saugroboter?
    Saugroboter arbeiten die Mehrheit im Stil von eines Teppichreinigers und nutzen dazu mehrere
    rotierende Bürsten bzw. Walzen, die mit Gummilippen versehen sind.

  12. Hello.This post was really motivating, especially since I was searching for thoughts on this matter last Friday.

  13. I simply couldn’t go away your site before suggesting that
    I actually enjoyed the usual info a person provide on your visitors?
    Is going to be again steadily in order to inspect new posts

  14. Hi, i feel that i noticed you visited my blog thus i came to return the desire?.I’m attempting to find issues to improve my web
    site!I suppose its good enough to use some of your concepts!!

  15. exciting review. As well visit our own site to obtain situs slot online.
    Fantastic site has received level of popularity from slot deposit pulsa tanpa potongan gaming men and women in Dalam negeri.
    Truth be told there tend to be numerous fascinating promos when you join currently.

  16. discuss表示:

    There is definately a lot to know about this subject.
    I love all the points you have made.

  17. zoe-beauty.be表示:

    Hi! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche.
    Your blog provided us valuable information to work on. You have done a extraordinary job!

  18. Its such as you read my mind! You seem to grasp a lot about this,
    such as you wrote the e book in it or something.
    I feel that you can do with some p.c. to drive the message home a little bit, but instead of that, this is fantastic blog.
    An excellent read. I’ll definitely be back.

  19. I like what you guys tend to be up too. This type of clever work and coverage!
    Keep up the good works guys I’ve added you guys to blogroll.

  20. Appreciate it for all your efforts that you have put in this.
    Very interesting information.

  21. Magnificent goods from you, man. I’ve understand your
    stuff previous to and you’re just extremely great.
    I really like what you’ve acquired here, really like what you’re stating and the way in which you say it.

    You make it entertaining and you still care for to keep
    it wise. I can’t wait to read far more from you. This is
    actually a terrific web site.

  22. I am regular visitor, how are you everybody?
    This piece of writing posted at this web page is genuinely
    good.

  23. Wellness Keto表示:

    Everything is very open with a clear explanation of the issues.
    It was definitely informative. Your site is very helpful.
    Thank you for sharing!

  24. My brother suggested I might like this web site. He used to be
    totally right. This post actually made my day. You can not believe
    just how so much time I had spent for this info! Thanks!

  25. Hi there! I could have sworn I’ve been to
    this site before but after checking through some of the post I realized it’s new to me.
    Anyhow, I’m definitely happy I found it and I’ll
    be bookmarking and checking back frequently!

  26. Hi! 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 valuable information to work
    on. You have done a wonderful job!

  27. As the admin of this site is working, no doubt very rapidly it will be renowned, due to its quality contents.

  28. toto slot表示:

    Hello there! 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 pleased I came across it
    and I’ll be bookmarking it and checking back frequently!

  29. Hai表示:

    Greetings! This is my 1st comment here so I just wanted to give a
    quick shout out and tell you I genuinely enjoy
    reading through your blog posts. Can you recommend any other blogs/websites/forums that deal with
    the same topics? Thanks!

  30. Greetings! I’ve been reading your blog for
    some time now and finally got the courage to go ahead and give you a shout out from New Caney
    Texas! Just wanted to tell you keep up the great job!

發佈留言

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