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,227,912 Responses

  1. Davidtes表示:

    tamoxifen endometrium tamoxifen buy how to lose weight on tamoxifen

  2. Why Nobody Cares About Auto Key Locksmith Locksmith Near Me Automotive

  3. Viola表示:

    Why Vehicle Diagnostics Will Be Your Next Big Obsession? diagnostic software [Viola]

  4. The 10 Most Terrifying Things About Car Replacement Key Cost
    car replacement Key Cost

  5. The Most Hilarious Complaints We’ve Seen About Upvc Window Repairs upvc window repairs near me

  6. 15 Things You Don’t Know About Window Repair Near window repair near me

  7. 15 Ideas For Gifts For The Cheap Online Electronics Shopping
    Uk Lover In Your Life Electrical Project Box

  8. An Easy-To-Follow Guide To Lock Smith For Car Locksmiths Car Keys Near Me

  9. gotogel表示:

    Situs Alternatif Gotogel Tools To Ease Your
    Daily Life Situs Alternatif Gotogel Trick That Everyone Should Learn gotogel

  10. Davidtes表示:

    aromatase inhibitor tamoxifen nolvadex pct raloxifene vs tamoxifen

  11. Davidtes表示:

    lisinopril in usa buy lisinopril 20 mg online united states cost of lisinopril 30 mg

  12. RandallTaulp表示:

    cost generic propecia without rx: cost of propecia without insurance – get generic propecia price

  13. If you’re trying to find a trusted option to sustain your weight monitoring trip, professionals highly recommend Keto XP Gummies for a factor. The one-of-a-kind combo of elements in these gummies has been very carefully crafted to aid your body in obtaining ketosis and also shedding body fat effectively. But that is actually merely the starting point. The benefits expand beyond weight loss, using a holistic technique to general welfare, https://songtr.ee/artist/10202249/johannaereed.

  14. The Good And Bad About Car Key Immobiliser Repair fob key repair Near me

  15. What Is Mesothelioma Claim And How To Make Use Of It Asbestos Lawsuit

  16. The 10 Most Dismal Double Glazed Windows Leeds Failures Of All Time
    Could Have Been Prevented Window doctor leeds (Images.google.com.pa)

  17. Your means of explaining everything in this piece of writing is actually fastidious,
    every one can easily understand it, Thanks a lot https://www.sosexpert.ca/

  18. Woah! I’m really loving the template/theme of this blog.

    It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between usability and appearance.
    I must say you’ve done a great job with this. Also, the blog loads super fast for me on Internet explorer.

    Excellent Blog!

    my homepage … 해외선물 커뮤니티

  19. How To Find Out If You’re All Set For Robot Vacuums Home robot vacuum

  20. fiat Car keys表示:

    Why Is It So Useful? In COVID-19? fiat Car keys

  21. WilliamRhype表示:

    http://ciprofloxacin.tech/# where can i buy cipro online

  22. A Step-By-Step Guide To Windows Repairs Near Me upvc door repairs near me

  23. 10 Facts About Best CSGO Opening Site That Can Instantly Put You In A Good Mood
    Counter-Strike Cases

  24. AndrewNuh表示:

    эффективное лечение алкоголизма в алматы https://someblog.ru/

  25. Ronaldpsymn表示:

    http://finasteride.store/# cost of generic propecia without rx

  26. The Secret Life Of Replacement Car Keys Ford replacement key Ford

  27. The 10 Most Terrifying Things About Mesothelioma Lawyer
    Mesothelioma Legal (Luxuriousrentz.Com)

發佈留言

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