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,482,522 Responses

  1. AlbertDuM表示:

    generic plavix: Clopidogrel Best Prices – cheap plavix antiplatelet drug

  2. AlbertDuM表示:

    cheapest Lisinopril: Lisinopril 1st – lisinopril price 10 mg

  3. Sazrwxx表示:

    Быстрая схема покупки диплома старого образца: что важно знать?
    gamesmaker.ru/forum/common/offtopics/#replyform

  4. Nikefut表示:

    dark web sites darknet drug market darkmarket url

  5. id=”firstHeading” class=”firstHeading mw-first-heading”>Search results

    Helρ

    Engkish

    Tools

    Tools
    mоvе to sidegar hide

    Actions

    Ԍeneral

    my site ร้านดอกไม้ด่วน

  6. The 2020 yr was hit quite exhausting by the worldwide pandemic Covid-19 and touring all around the world was restricted.

  7. Irene Beck表示:

    Can’t help but feel inspired seeing locals connect deeply around traditional practices; grateful always having guidance available dubai city tour

  8. Pol88表示:

    Useful information. Fortunate me I found your site by chance, and I’m surprised why this coincidence didn’t happened in advance!

    I bookmarked it.

  9. 밤문화表示:

    Exclusive Nightlife 밤문화

  10. AlbertDuM表示:

    Lisinopril 1st: cheapest Lisinopril – zestoretic 20

  11. BrandenSlomy表示:

    buy prednisone online uk: how much is prednisone 5mg – prednisone 10mg buy online

  12. Have you ever thought about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything. Nevertheless just imagine
    if you added some great pictures or videos to give your posts more,
    “pop”! Your content is excellent but with pics and videos, this blog could undeniably
    be one of the best in its field. Terrific blog!

  13. 다바오4989表示:

    Casino Blackjack Tips 다바오4989

  14. Beau表示:

    Your method of describing the whole thing in this article is really pleasant, every one can simply be aware of it, Thanks a lot https://www.cabanoneco.ca/

  15. Howdy, i read your blog from time to time and i
    own a similar one and i was just curious if you get a lot of spam comments?
    If so how do you stop it, any plugin or anything you can recommend?
    I get so much lately it’s driving me crazy so any assistance is very
    much appreciated.

  16. Hi just wanted to give you a brief heads up and let you know a few of
    the images aren’t loading properly. I’m not sure why
    but I think its a linking issue. I’ve tried it in two different
    web browsers and both show the same results.

  17. SEO Derry表示:

    I think what you said made a great deal of sense.
    But, what about this? what if you wrote a catchier post title?

    I am not suggesting your content is not good, but what if
    you added a post title that grabbed a person’s attention? I mean JS
    依照元素出現數量排序 – 馬老師 雲端研究室
    is a little vanilla. You ought to glance at Yahoo’s
    front page and watch how they create post titles to get viewers interested.

    You might try adding a video or a picture or two to get
    readers interested about what you’ve written. In my opinion, it might make your blog
    a little bit more interesting.

  18. DarrenUnjuh表示:

    http://lisinopril1st.com/# buy Lisinopril 1st

  19. Pretty nice post. I simply stumbled upon your blog and
    wanted to say that I have really enjoyed surfing around your weblog posts.
    In any case I will be subscribing on your rss feed and I’m hoping you write once more soon!

  20. Markdob表示:

    Приветствую. Подскажите, где найтиполезные блоги о недвижимости? Сейчас читаю – https://teplodom78.ru

  21. Randy Jenkins表示:

    I love how easy it is to book with P&J Cleaners cleaning service vancouver bc

  22. BernardMop表示:

    Plavix generic price PlavixClo Best Price buy plavix

  23. Anthonyshutt表示:

    Вы можете купить справки в Москве в нашей клинике https://aleksandriya-med.ru/.

  24. obviously like your web site but you have to check the
    spelling on quite a few of your posts. Several of them are rife with spelling problems and I find it very bothersome
    to tell the truth on the other hand I’ll certainly come again again.

  25. Just wish to say your article is as astounding. The clearness to your post is
    just nice and i could think you are knowledgeable in this subject.

    Well with your permission let me to clutch your feed to stay updated with forthcoming post.
    Thanks a million and please keep up the gratifying work.

發佈留言

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