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,446,146 Responses

  1. Salutations, colleague reader. I must commend the author for their discerning and artfully-composed blog post.
    The material was both illuminating and thought-provoking,
    leaving me with a deeper understanding of the subject at hand.

    I would aim to extend an invitation to engage with
    the distinguished PUSSY888 network. This domain offers a
    sphere of fun and intrigue, attracting those who esteem the more elegant things in life.

    I exhort you to immerse yourself in the extensive array and engross yourself in the
    electrifying adventures that summon you. Your participation would be extremely appreciated, and I await with
    impatience the prospect to interact with you more intimately within this prestigious online
    realm

    My page: gambling trends

  2. kypit 1s_ycEn表示:

    1с предприятие купить программу [url=www.svarog.forum24.ru/?1-0-0-00000330-000-0-0]www.svarog.forum24.ru/?1-0-0-00000330-000-0-0[/url] .

  3. AndreDib表示:

    my canadian pharmacy review https://indiancertpharm.com/# Online pharmacy
    india pharmacy

  4. 撥筋課程表示:

    https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-257/research/je-tall-sf-marketing-(469).html
    You should go for some bright colours, or if your daughter has a color picked out for you then go together with that.

  5. Hi I am so grateful I found your webpage, I really found you by mistake, while I was looking on Digg for something else, Anyhow I
    am here now and would just like to say thanks for a incredible post and a
    all round entertaining blog (I also love the theme/design), I don’t have time to look over it all at the moment
    but I have saved it and also included your RSS feeds, so when I have time I will
    be back to read a lot more, Please do keep up the fantastic job.

  6. Hi this is somewhat of off topic but I was wondering if blogs use WYSIWYG
    editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience.
    Any help would be enormously appreciated! http://Classicsmoothjazz.com/__media__/js/netsoltrademark.php?d=www.pnptech.co.kr%2Fbbs%2Fboard.php%3Fbo_table%3Dfree%26wr_id%3D531191

  7. AndreDib表示:

    pharmacy canadian https://indiancertpharm.shop/# Indian Cert Pharm
    п»їlegitimate online pharmacies india

  8. This web site really has all the info I needed concerning this subject and didn’t know who to ask.

  9. kypit 1s_khor表示:

    программа 1с купить с установкой программа 1с купить с установкой .

  10. Thanks for the practical tips. More at بلفاروپلاستی چیست

  11. I got this web site from my buddy who informed me about this site and at the moment this time I am
    browsing this website and reading very informative content here.

  12. kypit 1s_xtor表示:

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

  13. Brandonnot表示:

    canada ed drugs: Canadian Md Pharm – best canadian pharmacy to order from

  14. CurtisBam表示:

    canadian drugstore online: Canadian Md Pharm – reliable canadian pharmacy reviews

  15. CurtisBam表示:

    online canadian pharmacy review: CanadianMdPharm – best canadian pharmacy online

  16. atomcraft.ru表示:

    “Ask Me Anything”: Ten Answers To Your Questions About Pragmatic Korea 프라그마틱
    슬롯 환수율 (atomcraft.ru)

  17. Carrolleruck表示:

    https://mexicaneasypharm.shop/# Mexican Easy Pharm
    legal to buy prescription drugs from canada

  18. kypit 1s_shEn表示:

    1с предприятие купить программу https://svarog.forum24.ru/?1-0-0-00000330-000-0-0 .

  19. I pay a visit everyday a few blogs and sites
    to read posts, except this blog provides feature based content.

  20. Hello There. I found your blog using msn. This is a really well written article.
    I will make sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I will certainly comeback.

  21. kypit 1s_vior表示:

    настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .

  22. kypit 1s_flor表示:

    настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .

  23. buy viagra表示:

    Vi rekommenderar en strategi som riktar in sig på högsta möjliga volatilitet.

  24. kypit 1s_lpor表示:

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

發佈留言

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