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,231,340 Responses

  1. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen endometrium

  2. RandallTaulp表示:

    cytotec online: buy cytotec over the counter – Misoprostol 200 mg buy online

  3. Window Repair表示:

    5 Killer Quora Answers On Double Glazed Window Repair Window Repair

  4. I’m gone to tell my little brother, that he should also visit this weblog on regular basis to get updated from latest news.

  5. Why Locksmiths Cars Is Fastly Changing Into The Trendiest Thing In 2023
    cheapest locksmith for cars near me

  6. Why We Are In Love With Welcome To Avon (And You Should, Too!) welcome To avon

  7. Yes! Finally something about Explore Forex trading tutorials for beginners trading platforms.

  8. Davidtes表示:

    Cytotec 200mcg price buy cytotec over the counter cytotec abortion pill

  9. 7 Things About CS GO Case Battles You’ll Kick
    Yourself For Not Knowing Counter Strike Global

  10. I’d like to find out more? I’d love to find out some additional information.

    My site … 해외선물 커뮤니티

  11. The 10 Most Terrifying Things About Double Glazing Company Near Me double glazing Company near me

  12. Near By表示:

    How Volkswagen Polo Key Price Can Be Your Next Big Obsession Near By

  13. Davidtes表示:

    hysterectomy after breast cancer tamoxifen nolvadex 10mg tamoxifen mechanism of action

  14. What Is Double Glazed Window Leeds And How To Use It upvc window handle Repair leeds

  15. RandallTaulp表示:

    tamoxifen for gynecomastia reviews: how to lose weight on tamoxifen – tamoxifen generic

  16. I’ve been browsing online more than 3 hours today,
    yet I never found any interesting article like
    yours. It is pretty worth enough for me. Personally, if all web owners and bloggers made good content as you did, the internet will be much more useful
    than ever before.

  17. Valuable information. Fortunate me I discovered your web site by accident, and I
    am shocked why this coincidence didn’t came about earlier! I bookmarked it.

  18. Brock表示:

    The 3 Most Significant Disasters In Online Shopping Clothes Uk Cheap History 25-Foot Cat5E Cable, Brock,

  19. RandallTaulp表示:

    cost propecia no prescription: cost cheap propecia for sale – generic propecia tablets

  20. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get
    set up? I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web smart so I’m not 100% positive.
    Any suggestions or advice would be greatly appreciated.
    Thank you

  21. Buster表示:

    10 Things That Your Family Teach You About Window Replacement Near Me window replacement near me (Buster)

  22. Tressa表示:

    11 Methods To Redesign Completely Your Online Shopping Sites
    In Uk For Electronics Hydroponic Exhaust Fan (Tressa)

  23. How The 10 Worst Adhd Symptoms In Adults Test Fails Of All Time Could Have Been Prevented adhd in adults Symptoms and treatment

  24. Unexpected Business Strategies Helped What Is The Best
    Online Shopping In Uk Succeed Kalani 4-In-1 Crib

  25. Ronaldpsymn表示:

    http://lisinopril.network/# order lisinopril online

  26. Car Lovksmith表示:

    4 Dirty Little Tips On Emergency Car Locksmith And The Emergency
    Car Locksmith Industry Car Lovksmith

發佈留言

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