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,250,816 Responses

  1. each time i used to read smaller content which as well clear their motive, and that is also happening with this
    paragraph which I am reading at this place.

  2. Hello would you mind letting me know which hosting company you’re working with?
    I’ve loaded your blog in 3 different internet browsers and
    I must say this blog loads a lot quicker then most.

    Can you suggest a good internet hosting provider at a
    fair price? Many thanks, I appreciate it!

  3. Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how
    to get listed in Yahoo News? I’ve been trying for a while but I
    never seem to get there! Cheers

  4. beneficial review. Moreover visit one of the best
    site to understand agen slot pulsa. This web site has
    received likability from the comfort of slot machine game
    gaming customers in Indonesia. Generally there will be lots of
    interesting promos in case you join presently.

  5. Our objective is too make sure that your time
    at Caffe is seamless, and simple.

    Here is my web blog – 우리카지노 총판

  6. I love what you guys are usually up too. This sort of clever work and exposure!
    Keep up the superb works guys I’ve added you guys to my blogroll.

  7. You should take part in a contest for one of the highest
    quality sites on the web. I most certainly will
    recommend this blog!

  8. Somebody necessarily lend a hand to make critically articles I might state.
    This is the very first time I frequented your web page and up to now?
    I surprised with the analysis you made to make this particular post extraordinary.
    Wonderful task!

  9. I was curious if you ever thought of changing the page
    layout of your website? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way
    of content so people could connect with it better. Youve
    got an awful lot of text for only having one or 2 pictures.

    Maybe you could space it out better?

  10. I’m really enjoying the theme/design of your blog. Do you ever run into any browser compatibility issues?
    A handful of my blog readers have complained about my site not operating correctly
    in Explorer but looks great in Chrome. Do
    you have any solutions to help fix this problem?

  11. www.fcc.gov表示:

    I know this if off topic but I’m looking into starting my own blog and was curious what all is
    needed to get setup? I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web savvy so I’m not 100% certain. Any suggestions
    or advice would be greatly appreciated. Cheers

  12. Good article. I will be experiencing some of these issues as well..

  13. site表示:

    Hey! This is kind of off topic but I need some advice from an established
    blog. Is it tough to set up your own blog? I’m not very techincal
    but I can figure things out pretty quick. I’m thinking
    about making my own but I’m not sure where to begin. Do you have any points or suggestions?
    Appreciate it

  14. go now表示:

    I think everything wrote was very reasonable. However, what
    about this? what if you were to create a killer post title?
    I mean, I don’t wish to tell you how to run your blog, however what if you added something to possibly grab a person’s attention? I mean JS
    依照元素出現數量排序 – 馬老師 雲端研究室 is a
    little vanilla. You might look at Yahoo’s home page and see how they create news titles
    to grab viewers to click. You might try adding a video or a related pic or two to get readers interested about everything’ve got
    to say. In my opinion, it might bring your website a little
    bit more interesting.

  15. Hey there! I’ve been following your site
    for some time now and finally got the courage
    to go ahead and give you a shout out from Porter Texas! Just wanted
    to say keep up the great job!

  16. Outstanding quest there. What occurred after? Good luck!

  17. This excellent website truly has all of the information and facts I wanted about this
    subject and didn’t know who to ask.

  18. I’m really inspired together with your writing abilities and also with the format in your
    weblog. Is that this a paid subject matter or did you
    modify it yourself? Either way keep up the excellent high quality writing, it’s rare to see a great weblog like this one
    nowadays..

  19. sc.sie.gov.hk表示:

    Howdy! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really appreciate
    your content. Please let me know. Thank you

  20. web site表示:

    I am really impressed with your writing skills and also with the layout on your weblog.

    Is this a paid theme or did you customize it yourself?
    Anyway keep up the excellent quality writing,
    it’s rare to see a nice blog like this one these days.

  21. RUAY.COM表示:

    Informative article, totally what I needed.

  22. vimeo.com表示:

    If you desire to improve your know-how just keep visiting this site and be updated with the most recent gossip posted here.

  23. Excellent blog here! Also your web site loads up very fast!

    What host are you using? Can I get your affiliate link to your host?
    I wish my web site loaded up as quickly as
    yours lol

    Have a look at my blog … best knee supplements

  24. speedgh.com表示:

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

  25. This article is truly a good one it helps new internet users, who are
    wishing in favor of blogging.

  26. Terry表示:

    With havin so much content and articles do you ever run into any problems of plagorism or
    copyright infringement? My website has a lot of unique content I’ve either authored myself or outsourced but it appears a lot of it is popping it up all over the internet without my authorization. Do you know any methods to help reduce content from being ripped off?
    I’d really appreciate it.

  27. Hi mates, how is all, and what you desire to say regarding this piece
    of writing, in my view its in fact awesome in support
    side effects of glucosamine me.

  28. Article writing is also a excitement, if you be acquainted with then you can write if not it is complex to write.

  29. telugu mythri表示:

    Hey! I just wanted to ask if you ever have any issues with
    hackers? My last blog (wordpress) was hacked and I ended
    up losing a few months of hard work due to no back up. Do you
    have any methods to stop hackers?

發佈留言

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