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,448,379 Responses

  1. AndreDib表示:

    reddit canadian pharmacy https://canadianmdpharm.com/# canadian pharmacy com
    reputable indian pharmacies

  2. kypit 1s_doEn表示:

    программы фирмы 1с программы фирмы 1с .

  3. Guide To Bunk Beds Adults: The Intermediate Guide The Steps To Bunk Beds Adults Bunk beds Adults

  4. Gary McCoy表示:

    This was a fantastic resource. Check out عمل prk for more

  5. booster xt表示:

    It’s an amazing article in favor of all the web users;
    they will take advantage from it I am sure.

  6. Hello I am so thrilled I found your website, I really found you by mistake, while I was looking on Google
    for something else, Anyways I am here now and would just like to say
    cheers for a remarkable post and a all round
    thrilling blog (I also love the theme/design), I don’t have
    time to look over it all at the moment but I have book-marked
    it and also added your RSS feeds, so when I have time I will be
    back to read a lot more, Please do keep up the fantastic jo.

  7. Find out more表示:

    This piece of writing offers clear idea in support of the new visitors of blogging, that really how to do blogging and
    site-building.

  8. Thanks a lot for sharing this with all people you actually
    realize what you are talking about! Bookmarked.
    Kindly also discuss with my web site =). We can have a hyperlink
    alternate agreement among us

  9. Brandonnot表示:

    canada pharmacy: canadian pharmacy ratings – canadian pharmacy

  10. Rumpexbit App表示:

    hey there and thank you for your info – I have certainly picked up something new from right here.
    I did however expertise several technical points
    using this website, since I experienced to reload the website a lot of times previous to I could get it to load correctly.
    I had been wondering if your hosting is OK? Not that I am
    complaining, but slow loading instances times will often affect your placement in google and could damage
    your high-quality score if advertising and marketing with Adwords.
    Anyway I’m adding this RSS to my email and can look out for a
    lot more of your respective interesting content. Make
    sure you update this again very soon.

  11. I’ll immediately grab your rss feed as I can’t in finding your email subscription link or e-newsletter service.
    Do you’ve any? Kindly permit me realize in order that I could subscribe.

    Thanks.

  12. What’s Going down i’m new to this, I stumbled upon this
    I have discovered It absolutely helpful and it has helped me out loads.
    I’m hoping to give a contribution & aid different users like its helped me.
    Good job. http://aydin.Ogo.org.tr/question/trouver-un-nid-de-fourmis-dans-la-maison-guide-pratique-8/

  13. kypit 1s_wcor表示:

    программа 1с купить с установкой https://www.svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513 .

  14. kypit 1s_jnEn表示:

    купить 1с бухгалтерия купить http://www.svarog.forum24.ru/?1-0-0-00000330-000-0-0 .

  15. kypit 1s_haor表示:

    программа 1с купить с установкой svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513 .

  16. CurtisBam表示:

    Mexican Easy Pharm: mexican drugstore online – Mexican Easy Pharm

  17. kypit 1s_uuEn表示:

    1с предприятие купить программу 1с предприятие купить программу .

  18. Brandonnot表示:

    canada drug pharmacy: pharmacy canadian – canadian pharmacy no scripts

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

  20. Carrolleruck表示:

    https://indiancertpharm.shop/# Online pharmacy
    canada drugs online

  21. Brandonnot表示:

    pharmacies in mexico that ship to usa: reputable mexican pharmacies online – purple pharmacy mexico price list

  22. Guide To Emergency Patio Door Repair: The Intermediate Guide The Steps To Emergency Patio
    Door Repair Emergency Patio Door Repair

  23. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my
    newest twitter updates. I’ve been looking for a plug-in like this for
    quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I
    look forward to your new updates.

  24. kypit 1s_dcEn表示:

    1с бухгалтерия купить 1с бухгалтерия купить .

  25. In real life, a large percentage of subject areas are even more
    difficult than a common amateur observer could decide on, in accordance with their vantagepoint.
    I’m not expressing that I happen to be a professional
    person on this particular issue at-hand, as a result I reckon it’s for different community participants to give some thought to.

    I am not trying to make difficulties or be maddening. Instead, I know from experience
    that the aforementioned might be the case. I am skilled in Perinatal Massage Therapy, and in my personal selected vocation, I experience it very much.
    Just-graduated Postnatal Massage Practitioners are apt to overpromise;
    which can be, they don’t yet genuinely appreciate
    the boundaries of their “scope of practice,” and as a
    result they can make claims that are too general when speaking with
    patients. It’s the comparable occurrence; they have been minimally schooled in a
    theme, don’t comprehend the thorough amount of the idea, and
    now (wrongly) conclude they are the Experts.

  26. memek becek表示:

    What’s up, all is going perfectly here and ofcourse every one is sharing data, that’s actually good, keep up writing.

  27. Sweet blog! I found it while searching on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Appreciate it

發佈留言

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