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,232,462 Responses

  1. I don’t even know how I finished up here, however I thought this put up was great.
    I do not recognise who you are but certainly you’re going
    to a famous blogger when you are not already. Cheers!

  2. Hi there to all, how is all, I think every one is getting more
    from this website, and your views are pleasant in favor of new users.

  3. Hey very cool website!! Man .. Excellent ..
    Amazing .. I’ll bookmark your blog and take the feeds additionally?
    I am happy to find numerous helpful information here in the put up, we’d like develop more techniques on this regard, thank you
    for sharing. . . . . .

  4. I’m truly enjoying the design and layout of your website. It’s a very easy on the eyes which makes it much more pleasant for me
    to come here and visit more often. Did you hire out a designer to create
    your theme? Fantastic work!

  5. Hmm is anyone else experiencing problems with the
    images on this blog loading? I’m trying to figure
    out if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.

  6. discuss表示:

    I’d like to thank you for the efforts you’ve put in penning this website.
    I really hope to see the same high-grade blog posts by you
    later on as well. In fact, your creative writing abilities has motivated me to get
    my own site now 😉

  7. A motivating discussion is worth comment. I do think that you need
    to write more on this issue, it might not be a taboo matter but typically people
    don’t discuss such subjects. To the next! Many thanks!!

  8. myspace.com表示:

    Hi, I do believe this is an excellent blog. I stumbledupon it 😉 I will return yet again since i have
    book marked it. Money and freedom is the best way to change, may
    you be rich and continue to help other people.

  9. Spot on with this write-up, I really think this website needs much more attention.
    I’ll probably be returning to see more, thanks
    for the information!

  10. I seriously love your website.. Very nice colors & theme.
    Did you develop this site yourself? Please reply back as I’m
    attempting to create my very own website and would like to know
    where you got this from or just what the theme is
    called. Appreciate it!

  11. Hello there! I simply would like to offer you a big thumbs
    up for the excellent info you have got here on this post.
    I’ll be returning to your website for more soon.

  12. Good day! Do you use Twitter? I’d like to follow you if that would be okay.

    I’m definitely enjoying your blog and look forward to new posts.

  13. I loved as much as you will receive carried out right here.

    The sketch is attractive, your authored material stylish.
    nonetheless, you command get got an impatience over that you
    wish be delivering the following. unwell unquestionably come more formerly
    again as exactly the same nearly very often inside case
    you shield this hike.

  14. I really like what you guys tend to be up too. This sort of
    clever work and reporting! Keep up the terrific works guys I’ve
    you guys to my personal blogroll.

  15. This blog was… how do I say it? Relevant!!
    Finally I have found something that helped me. Kudos!

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

  17. I was recommended this website by means of my cousin. I am not positive whether or
    not this put up is written by way of him as no one else recognise such special approximately my problem.
    You are wonderful! Thanks!

  18. 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, really like what you’re saying and the way in which you
    say it. You make it enjoyable and you still take care of
    to keep it smart. I cant wait to read much more from you.
    This is actually a terrific website.

  19. Wow, that’s what I was seeking for, what a material! existing here at this website, thanks admin of this
    web site.

  20. Hello are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and
    create my own. Do you need any html coding knowledge to make
    your own blog? Any help would be greatly appreciated!

  21. Hello to every body, it’s my first go to see of this weblog;
    this website includes amazing and in fact excellent information in favor of
    readers.

  22. Thank you, I have just been searching for information about this subject for a long time
    and yours is the greatest I’ve came upon till now.
    But, what in regards to the bottom line? Are you positive in regards to the
    supply?

  23. Nice post. I was checking constantly this blog and I am impressed!

    Extremely useful info specially the last part 🙂 I care for such information a lot.

    I was seeking this certain info for a very long time.
    Thank you and best of luck.

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

  25. Great blog here! Also your site loads up very fast! What host are you
    using? Can I get your affiliate link to your host?

    I wish my website loaded up as fast as yours lol

  26. Thanks , I have just been looking for information about this subject
    for a long time and yours is the best I’ve came upon till now.
    But, what concerning the conclusion? Are you positive about the supply?

  27. I like what you guys are usually up too. Such clever work and coverage!
    Keep up the amazing works guys I’ve incorporated you guys to blogroll.

  28. Kathaleen表示:

    I do not even know how I ended up here, but I thought this post was great.
    I don’t know who you are but definitely you are
    going to a famous blogger if you are not already 😉
    Cheers!

發佈留言

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