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,234,695 Responses

  1. Porn表示:

    Remarkable! Its in fact awesome post, I have got much clear idea
    concerning from this article.

  2. Helpful info. Fortunate me I found your web site by accident, and I’m
    stunned why this accident didn’t happened earlier!
    I bookmarked it.

  3. rpg game表示:

    A motivating discussion is definitely worth comment.
    I do think that you ought to publish more on this issue, it may not be a taboo matter but typically folks don’t talk about these issues.

    To the next! Best wishes!!

  4. siabgear.com表示:

    It’s the best time to make some plans for the future and
    it is time to be happy. I’ve read this post and if I could I want to suggest
    you some interesting things or tips. Perhaps you could write next articles referring to this article.
    I want to read even more things about it!

  5. Darrelherse表示:

    slot siteleri 2024: canl? slot siteleri – slot siteleri

  6. Darrelherse表示:

    en iyi slot siteler: slot oyun siteleri – deneme bonusu veren siteler

  7. 7 Simple Strategies To Completely Refreshing Your Mesothelioma Lawsuits Mesothelioma Attorney

  8. Darrelherse表示:

    deneme veren slot siteleri: yasal slot siteleri – yeni slot siteleri

  9. KevinSounc表示:

    http://slotsiteleri.bid/# slot siteleri guvenilir

  10. cheap flight表示:

    I visit everyday some sites and sites to read posts, however this blog provides feature based
    writing.

  11. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza free spin demo

  12. Hi there, for all time i used to check blog posts here early in the dawn, since i
    like to find out more and more.

  13. 5 Must-Know-Practices Of Mesothelioma For 2023 asbestos-related Cancer attorney

  14. happiness表示:

    Pretty nice post. I just stumbled upon your weblog
    and wished to say that I have truly enjoyed surfing
    around your blog posts. In any case I will be subscribing
    to your rss feed and I hope you write again soon!

  15. Eanrlta表示:

    Здравствуйте!
    Мы изготавливаем дипломы любой профессии по доступным тарифам.
    asteroid-bowling.ru/index

  16. It is truly a great and helpful piece of info. I am glad that
    you shared this useful information with us.
    Please stay us informed like this. Thank you for sharing.

  17. Aw, this was a very nice post. Finding the time and actual effort to generate a very good article… but what can I say… I hesitate a lot and never seem to get nearly anything done.

  18. Lilian表示:

    Greetings! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly?
    My site looks weird when viewing from my iphone. I’m
    trying to find a theme or plugin that might be able to resolve this issue.
    If you have any recommendations, please share. Thank you!

  19. 15 Tips Your Boss Wished You’d Known About Mesothelioma Lawyer Mesothelioma law advocate

  20. Justin表示:

    Fantastic blog! Do you have any tips for aspiring writers?
    I’m planning to start my own site soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or go for a paid option?
    There are so many options out there that I’m completely confused ..
    Any suggestions? Appreciate it!

  21. Jame表示:

    Wow, this article is nice, my sister is analyzing these kinds of
    things, thus I am going to convey her.

  22. Learn Code表示:

    Excellent weblog here! Also your website rather a lot up fast!
    What web host are you the usage of? Can I am getting
    your associate hyperlink to your host? I wish my
    site loaded up as fast as yours lol

  23. Everything is very open with a precise clarification of the
    issues. It was definitely informative. Your website is very helpful.
    Many thanks for sharing!

  24. KevinSounc表示:

    http://denemebonusuverensiteler.win/# deneme bonusu veren siteler

  25. Thurman表示:

    fantastic post, very informative. I’m wondering why the other specialists
    of this sector do not realize this. You should continue
    your writing. I am confident, you have a huge readers’ base already!

  26. Very soon this web page will be famous among all blog people, due to
    it’s pleasant content

發佈留言

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