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,523,712 Responses

  1. Treftal表示:

    Официальная покупка диплома ПТУ с упрощенной программой обучения

    paladiny.ru/forummess.dwar.php?TopicID=28716

  2. BrandonSaisy表示:

    erection pills that work: cheap drugs – online drugstore

  3. 9 Lessons Your Parents Taught You About Daftar Akun Togel Resmi daftar Akun togel resmi

  4. LeroyBak表示:

    ed pills online pharmacy cheap drugs buy prescription drugs online without

  5. 14 Smart Ways To Spend Your The Leftover
    Psychiatrist Near Me Private Budget Private adhd Psychiatrist

  6. 10 Things That Your Family Taught You About Replacement Key For Renault Trafic Van replacement key For renault Trafic van

  7. slot gacor表示:

    Appreciating the hard work you put into your blog and in depth information you provide.
    It’s good to come across a blog every once in a while
    that isn’t the same old rehashed material. Excellent read!
    I’ve saved your site and I’m including your RSS feeds to my Google
    account.

  8. slot表示:

    Good post. I learn something new and challenging on blogs I stumbleupon everyday.
    It will always be interesting to read content from other writers
    and use a little something from other websites.

  9. The Key Smith For Cars Awards: The Most Sexiest, Worst, And Strangest Things We’ve Ever Seen locksmith For Cars

  10. Who’s The World’s Top Expert On Portable Electric Wheelchair?
    Lightweight Power Chair

  11. Wow! At last I got a webpage from where I know how to genuinely
    take helpful facts concerning my study and knowledge.

  12. I got this site from my buddy who shared with me regarding
    this website and now this time I am visiting this
    site and reading very informative articles or reviews here.

  13. BrandonSaisy表示:

    mexican rx online: mexico pharmacy – mexico pharmacies prescription drugs

  14. LeroyBak表示:

    pharmacy medications cheap medication ways to treat erectile dysfunction

  15. Private ADHD Strategies That Will Change Your Life private adhd assessment glasgow cost (baidubookmark.com)

  16. Ищете грузоперевозки Новосибирск Абакан? Мы предложим вам выгодные условия для доставки.

  17. Hello, I read your new stuff daily. Your writing style is awesome, keep it up!

  18. LeroyBak表示:

    canadian online drugs cheap medication erection problems

  19. The Steve Jobs Of Private ADHD Meet Your Fellow Private ADHD Enthusiasts.
    Steve Jobs Of The Private ADHD Industry private adhd assessment ireland adult

  20. LeroyBak表示:

    ed meds online pharmacy cheap meds dog antibiotics without vet prescription

  21. LeroyBak表示:

    buying prescription drugs in mexico mexican pharmacy pharmacies in mexico that ship to usa

  22. Great beat ! I would like to apprentice at the same time as you amend your website, how could i
    subscribe for a blog web site? The account helped me a acceptable deal.
    I have been a little bit acquainted of this your broadcast provided shiny clear concept

  23. I was able to find good information from your blog articles.

  24. Jorgeagopy表示:

    online shopping pharmacy india: Best Indian pharmacy – cheapest online pharmacy india

  25. St. Thomas the Apostle Boston Highway, London Borough of Ealing Stained glass Moira Forsyth made stained glass for the youngsters’s chapel and small panels on the west finish of the church, “Suffer Little Youngsters”, which was made in Arts and Crafts model.

  26. KevinFiels表示:

    Recent drug developments. Drug guide provided.
    buy diflucan online without prescription
    Medicine overview available. Pill information provided.

發佈留言

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