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,447,097 Responses

  1. вывод из запоя круглосуточно нижний новгород вывод из запоя круглосуточно нижний новгород .

  2. CurtisBam表示:

    Mexican Easy Pharm: mexican pharmaceuticals online – pharmacies in mexico that ship to usa

  3. Brandonnot表示:

    legitimate canadian pharmacy online: Canadian Md Pharm – best rated canadian pharmacy

  4. Usually I don’t learn article on blogs, however I would
    like to say that this write-up very forced me to check out
    and do so! Your writing style has been amazed me.
    Thanks, quite great post.

  5. Reinaldo表示:

    The Next Big New Uk ADHD Medication Industry medication for adhd and anxiety in adults – Reinaldo,

  6. Brandonnot表示:

    canadapharmacyonline com: Canadian Md Pharm – canadian pharmacy prices

  7. Thanks for sharing your thoughts about i want to apply for e-visa.
    Regards

  8. SEO Optimizer Software Tools To Ease Your
    Daily Lifethe One SEO Optimizer Software Trick
    That Everybody Should Be Able To Seo optimizer software

  9. kypit 1s_loEn表示:

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

  10. kypit 1s_awEn表示:

    купить 1с бухгалтерия 8.3 проф версия цена купить 1с бухгалтерия 8.3 проф версия цена .

  11. RileyBok表示:

    Viagra * Cialis * Levitra

    All the products you are looking an eye to are currently available for the duration of 1+1.

    4 more tablets of unified of the following services: Viagra * Cialis * Levitra

    https://xn--2i0bm4p0sf2wh7vdmsy.net

  12. kypit 1s_ttEn表示:

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

  13. AndreDib表示:

    canadian discount pharmacy https://indiancertpharm.com/# Online pharmacy
    best india pharmacy

  14. Brandonnot表示:

    reputable canadian pharmacy: Canadian Md Pharm – best canadian pharmacy online

  15. Hey there would you mind sharing which blog platform you’re working with?
    I’m looking to start my own blog soon but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your design and style seems different then most blogs
    and I’m looking for something completely unique.
    P.S Sorry for being off-topic but I had to ask! https://Oldchicken.kr/bbs/board.php?bo_table=sub0601&wr_id=32397

  16. Hmm is anyone else having problems with the images on this
    blog loading? I’m trying to determine if its a
    problem on my end or if it’s the blog. Any feedback would be greatly appreciated.

  17. find this表示:

    Hey there! Would you mind if I share your blog with my twitter group?
    There’s a lot of people that I think would really appreciate your content.

    Please let me know. Cheers

  18. kypit 1s_eror表示:

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

  19. вывод из запоя в стационаре воронежа http://vyvod-iz-zapoya-v-stacionare-voronezh23.ru/ .

  20. kill表示:

    This site truly has all of the information and facts I needed about this subject and didn’t know who
    to ask.

  21. kinggame365表示:

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

    Help

    English

    Tools

    Tools
    move to sidebar hide

    Actions

    General

  22. The Little-Known Benefits Of Ai Rewrite Text Use ai to rewrite text

發佈留言

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