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,365,657 Responses

  1. Davidtes表示:

    nolvadex vs clomid does tamoxifen cause weight loss tamoxifen therapy

  2. 17 Signs To Know You Work With Side By Side Fridge Freezer Under Counter small side by side Refrigerator freezer

  3. Ronaldpsymn表示:

    http://finasteride.store/# order cheap propecia without prescription

  4. Are You Sick Of Double Glazing In Leeds? 10 Inspirational Sources That Will
    Revive Your Love For Double Glazing In Leeds french door Repair leeds,
    http://www.meiyingge8.com,

  5. Ronaldpsymn表示:

    https://lisinopril.network/# lisinopril 15 mg tablets

  6. Davidtes表示:

    cipro pharmacy buy generic ciprofloxacin ciprofloxacin

  7. telegra.Ph表示:

    This History Behind Bunk Beds Will Haunt You Forever! Bunk beds sales (telegra.Ph)

  8. Davidtes表示:

    buy misoprostol over the counter cytotec buy online usa buy cytotec over the counter

  9. Used Mobility Scooters For Sale Near Me Tools To Help You Manage
    Your Daily Lifethe One Used Mobility Scooters For Sale Near Me
    Trick Every Person Should Be Able To mobility scooters for sale Near me

  10. Davidtes表示:

    buy cytotec over the counter buy cytotec buy cytotec

  11. Davidtes表示:

    lisinopril 10mg lisinopril 20 mg india zestril 20 mg price canadian pharmacy

  12. WilliamRhype表示:

    https://finasteride.store/# propecia cost

  13. Damaged表示:

    What Is Cost Of A Replacement Car Key And How To Utilize
    What Is Cost Of A Replacement Car Key And How To Use Damaged

  14. 15 Up-And-Coming ADHD Private Assesment Bloggers
    You Need To Check Out private adhd assessment cost uk

  15. RandallTaulp表示:

    tamoxifen cyp2d6: nolvadex pct – should i take tamoxifen

  16. 221bakersorg表示:

    Wonderful beat ! I would like to apprentice while you amend your web site, how can i subscribe for a blog website?
    The account aided me a acceptable deal. I had been tiny bit acquainted
    of this your broadcast offered bright clear idea

  17. RandallTaulp表示:

    buy tamoxifen: nolvadex online – tamoxifen estrogen

  18. Davidtes表示:

    tamoxifen citrate tamoxifen buy where can i buy nolvadex

  19. Online Shopping Website In London Tools To Ease Your
    Everyday Lifethe Only Online Shopping Website In London Trick Every Person Should
    Learn Online Shopping website in london

  20. Terrific postings, Many thanks.

  21. dior gacor表示:

    I visited various sites except the audio feature
    for audio songs current at this web page is genuinely wonderful.

  22. gay porn表示:

    I like the helpful info you provide in your articles.
    I’ll bookmark your blog and check again here frequently.
    I am quite sure I’ll learn a lot of new stuff right here! Best of
    luck for the next!

  23. 9 Things Your Parents Teach You About Situs Alternatif Gotogel situs alternatif gotogel

  24. What Is It That Makes Memory Foam Mattresses Double So Popular?
    Mattress double price

  25. Martina表示:

    It Is The History Of Mental Health Check Up In 10
    Milestones mental health assessment report (Martina)

  26. I was recommended this website by my cousin. I’m not sure whether this post is written by him as no one
    else know such detailed about my difficulty.
    You’re amazing! Thanks!

  27. What Ford Key Replacement Experts Want You To Learn Ford ka key replacement

  28. agriexpert.kz表示:

    Twenty Myths About Self Emptying Vacuum: Busted
    self-emptying robot vacuum (agriexpert.kz)

  29. Also visit my web blog; sex on facebook

  30. Childrens Double Bunk Bed Explained In Less Than 140 Characters Double bunk beds With mattress

發佈留言

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