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

911,951 Responses

  1. Good way of telling, and pleasant article to obtain data about my presentation subject matter, which i am going to convey in university.

  2. huku.fool.jp表示:

    What’s up to every one, the contents existing at this
    site are actually awesome for people knowledge, well, keep up
    the nice work fellows.

  3. adidasgazelle表示:

    This is really interesting, You are a very skilled blogger.

    I’ve joined your rss feed and look forward
    to seeking more of your wonderful post. Also, I have shared your
    web site in my social networks!

  4. You can definitely see your skills in the article you write.

    The arena hopes for even more passionate writers such as you who are not afraid to say how they believe.

    All the time go after your heart.

  5. Thanks a bunch for sharing this with all people you actually realize what you’re
    speaking about! Bookmarked. Please also talk over with my site =).
    We can have a hyperlink exchange contract among
    us

  6. Thanks for sharing your thoughts on situs slot online indonesia.
    Regards

  7. I constantly emailed this weblog post page to all my contacts, for the reason that if like
    to read it afterward my links will too.

  8. Howdy! I’m at work browsing your blog from my new iphone 3gs!
    Just wanted to say I love reading your blog and look
    forward to all your posts! Carry on the superb work!

  9. I know this web page provides quality depending articles and additional data, is there any other site
    which presents these things in quality?

  10. What’s up, after reading this remarkable post i am as well happy to share my familiarity here with friends.

  11. I love what you guys tend to be up too. This kind of clever work and reporting!

    Keep up the excellent works guys I’ve included you guys to blogroll.

  12. Shela表示:

    Hey this is kinda of off topic but I was wanting to know
    if blogs use WYSIWYG editors or if you have to manually code with
    HTML. I’m starting a blog soon but have no coding know-how so I wanted
    to get advice from someone with experience. Any help would be greatly
    appreciated!

  13. www.fcc.gov表示:

    I absolutely love your site.. Pleasant colors & theme.
    Did you create this amazing site yourself? Please reply
    back as I’m planning to create my own site and would
    like to know where you got this from or exactly what the theme is named.

    Thank you!

  14. pastelink.net表示:

    Hello! I could have sworn I’ve been to this website before but after reading 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 often!

  15. Wow, this article is pleasant, my sister is analyzing these things, thus I am going to let
    know her.

  16. I could not refrain from commenting. Exceptionally well written!

  17. customs表示:

    Hey! I’m at work browsing your blog from my new iphone 3gs!
    Just wanted to say I love reading your blog and look
    forward to all your posts! Carry on the outstanding
    work!

  18. Your method of describing the whole thing in this
    article is actually pleasant, all be capable of effortlessly know it, Thanks a lot.

  19. Oh my goodness! Incredible article dude! Thanks, However I am having issues with
    your RSS. I don’t understand the reason why I am unable to subscribe to
    it. Is there anybody having the same RSS problems? Anyone that knows the solution will you kindly respond?
    Thanks!!

  20. This post will assist the internet viewers for setting up new webpage
    or even a weblog from start to end.

  21. whoah this weblog is magnificent i like studying your posts.
    Keep up the good work! You recognize, a lot of persons are
    searching round for this information, you could help them greatly.

  22. Hello there! I know this is kind of off topic but I was wondering if you knew where I could find a
    captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

  23. Good day! I know this is somewhat off topic but
    I was wondering which blog platform are you using for this site?
    I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking
    at alternatives for another platform. I would be great if you could
    point me in the direction of a good platform.

  24. This info is invaluable. When can I find out more?

  25. I am truly pleased to read this web site posts which contains tons of useful
    data, thanks for providing these kinds of
    statistics.

  26. zalicz.net表示:

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

  27. Wow that was strange. I just wrote an incredibly
    long comment but after I clicked submit my comment didn’t show up.
    Grrrr… well I’m not writing all that over again. Anyway, just wanted to say superb blog!

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

  29. magic.ly表示:

    First of all I want to say fantastic blog! I had a quick question in which
    I’d like to ask if you don’t mind. I was curious to know how you
    center yourself and clear your thoughts before writing.

    I’ve had a difficult time clearing my mind in getting my ideas
    out there. I do take pleasure in writing however
    it just seems like the first 10 to 15 minutes are generally wasted simply just trying to figure out how to begin. Any recommendations
    or tips? Appreciate it!

    My web site slot367 (magic.ly)

  30. Willian表示:

    Wow! This blog looks just like my old one! It’s on a completely different topic
    but it has pretty much the same page layout and
    design. Superb choice of colors!

發佈留言

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