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

841,376 Responses

  1. If you are going for best contents like I do, only go
    to see this website all the time for the reason that it offers feature contents, thanks

  2. Hello, I think your blog might be having browser compatibility issues.

    When I look at your blog site in Safari, it
    looks fine but when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up! Other then that, great blog!

  3. What’s up Dear, are you genuinely visiting this website on a regular
    basis, if so then you will definitely get pleasant knowledge.

  4. I enjoy, lead to I found exactly what I was having a look for.
    You have ended my four day long hunt! God Bless you man. Have a great day.

    Bye

  5. Have you ever thought about writing an e-book or guest authoring on other sites?
    I have a blog centered on the same information you discuss and would love to have you
    share some stories/information. I know my audience would value your work.

    If you are even remotely interested, feel free to send
    me an e-mail.

  6. Juliet表示:

    Post writing is also a excitement, if you be familiar with after that you
    can write or else it is complicated to write.

  7. I’ll right away clutch your rss as I can not
    to find your e-mail subscription hyperlink or newsletter service.

    Do you’ve any? Kindly let me realize so that I may subscribe.
    Thanks.

  8. Very quickly this website will be famous amid all blogging people, due to it’s fastidious content

  9. Howdy 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 guidance from someone with experience.

    Any help would be enormously appreciated!

  10. For most up-to-date news you have to pay a quick
    visit world wide web and on the web I found this site as a finest website for most up-to-date updates.

  11. discuss表示:

    Excellent beat ! I wish to apprentice while you amend your site, how could i
    subscribe for a blog web site? The account helped me
    a acceptable deal. I had been a little bit acquainted of
    this your broadcast provided bright clear idea

  12. discuss表示:

    Hello, just wanted to say, I enjoyed this article.
    It was inspiring. Keep on posting!

  13. I always used to read post in news papers but now as I am a user of web therefore from now I am using net for posts, thanks
    to web.

  14. linktr.ee表示:

    Good post. I learn something new and challenging on blogs I stumbleupon every
    day. It’s always exciting to read articles from other authors and use a little something
    from other sites.

  15. Wow, this piece of writing is fastidious, my sister is analyzing these things, so I am going to convey
    her.

  16. Hello there, I discovered your website by the use of Google whilst searching for a comparable matter, your website came up, it looks great.

    I have bookmarked it in my google bookmarks.
    Hi there, just was alert to your weblog through Google, and found that it is truly informative.

    I am gonna watch out for brussels. I’ll be grateful should you continue this in future.
    Many other folks will probably be benefited from your writing.

    Cheers!

  17. Hi my loved one! I want to say that this post is awesome,
    nice written and come with approximately all significant infos.
    I’d like to peer extra posts like this .

  18. WOW just what I was searching for. Came here by searching for situs judi
    slot online

  19. It’s remarkable to pay a visit this website and reading the
    views of all friends concerning this paragraph, while I am also zealous of getting know-how.

  20. Fantastic site. Lots of useful info here. I am sending it to a few friends
    ans additionally sharing in delicious. And obviously, thank you for your effort!

  21. I think this is among the most significant information for me.
    And i am glad reading your article. But want to remark on some general things,
    The website style is perfect, the articles is really great : D.
    Good job, cheers

  22. Hey There. I found your blog the usage of msn. This is an extremely neatly written article.
    I’ll make sure to bookmark it and come back to learn more of your helpful information.
    Thanks for the post. I’ll certainly return.

  23. Auf der Berliner Maker Faire fiel im Oktober 2016 mit einem Hackathon der Startschuss für “Made for my Wheelchair”.
    Hier trafen Rollstuhlfahrer auf Techniker und Designer, um gemeinsam, aber nutzerzentriert Ideen für Add-ons zu sammeln, die das
    Leben hiermit Rollstuhl einfacher machen. Als inhaltliche Schwerpunkte wurden nach der Ideenfindung Transport und Stauraum, Fahren im Schnee und Beleuchtung ausgewählt, um mehr Sicherheit und
    Mobilität bei schwierigen Verkehrsverhältnissen zu ermöglichen. Zu den getesteten Produkten zählten nicht
    zuletzt Schneeketten, LED-Leuchten und Anhänger. Da die fertigen Anleitungen mit möglichst geringem finanziellen und zeitlichen Aufwand umgesetzt und angepasst werden sollen, setzten sich schließlich die
    Beleuchtung (Open Lights) und der Anhänger (Open Trailer) bei den Projektteilnehmern durch.
    Hernach wurden die Projekte in mehreren Workshops ausprobiert und überarbeitet, während das Fablab Berlin jeweils die Herstellung der Prototypen übernahm.
    Damit Anhänger am Rollstuhl wird es einfacher, eine Begleitung oder schwere, unhandliche Dinge zu transportieren. Die Lichter umsorgen mehr Sichtbarkeit im Straßenverkehr.
    Basierend auf Adafruits Neopixeln und dem Mikrocontroller Trinket lässt sich die LED-Leuchte individuell programmieren und erweitern. Im Projekt wurden zwei Designs entwickelt: ein Streifen, der als Front-
    und Unterbodenbeleuchtung angebracht wird, sowie ein Ring als Rückleuchte.

  24. It’s an awesome article for all the internet viewers;
    they will get benefit from it I am sure.

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

  26. Thank you for some other wonderful article. The place else may just anyone get that type of info in such
    an ideal method of writing? I’ve a presentation next week, and I’m at
    the look for such info.

  27. Nice answers in return of this matter with genuine arguments and explaining
    the whole thing regarding that.

  28. Great web site you’ve got here.. It’s hard to find good quality
    writing like yours these days. I truly appreciate
    individuals like you! Take care!!

  29. Quality articles is the important to interest
    the visitors to go to see the website, that’s what this website
    is providing.

  30. You should be a part of a contest for one of the finest blogs on the net.
    I’m going to highly recommend this site!

發佈留言

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