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,234,854 Responses

  1. vigrx plus表示:

    My spouse and I stumbled over here by a different website and thought I may as
    well check things out. I like what I see so now i am following
    you. Look forward to going over your web page repeatedly.

  2. 12 Stats About Injury Litigation To Make You Think About The Other People key west injury Law firm

  3. TylerPhona表示:

    https://cialist.pro/# Generic Tadalafil 20mg price

  4. 15 Gifts For The Online Grocery Stores That Ship Lover In Your Life Fitness Equipment Pulley

  5. 15 Presents For Your Replacement Windows Leeds Lover In Your Life Window doctor leeds

  6. Where Can You Get The Most Reliable Beko Washing Machines Uk Information?

  7. An Easy-To-Follow Guide To Windows Repairs
    Near Me window replacement

  8. 20 Resources That Will Make You Better At Uk Online Grocery Shopping
    Sites Barista Cleaning Tablets

  9. ElmerWes表示:

    Levitra 20 mg for sale Levitra 20mg price Buy Vardenafil 20mg online

  10. ElmerWes表示:

    cheap kamagra kamagra.win п»їkamagra

  11. Great delivery. Outstanding arguments. Keep up the great spirit.

    Feel free to surf to my site Rx 2 Go Pharmacy

  12. Lslv168.Com表示:

    10 CSGO Cases Opening Related Projects That Can Stretch Your Creativity Web
    Case (Lslv168.Com)

  13. Wood Burner: 11 Thing You’re Not Doing wood burner Clearance Sale

  14. The Comprehensive Guide To Upvc Window Repairs Upvc window repairs near me

  15. ElmerWes表示:

    Buy Vardenafil 20mg levitrav.store Vardenafil buy online

  16. ElmerWes表示:

    sildenafil over the counter Cheap Viagra 100mg sildenafil 50 mg price

  17. otuslot表示:

    Right now it looks like BlogEngine is the preferred blogging
    platform available right now. (from what I’ve read) Is that what you’re using on your
    blog?

  18. Eu recomendo este cassino blaze apostas

  19. GeorgeSep表示:

    buy kamagra online usa: kamagra pills – п»їkamagra

  20. Where Will Online Shopping Clothes Uk Cheap Be 1 Year From Today?
    Lucky Line Magnetic Key Box

  21. ElmerWes表示:

    Cheap Levitra online Buy generic Levitra online Buy Levitra 20mg online

  22. DavidOwelo表示:

    https://cenforce.pro/# buy cenforce

  23. I like what you guys tend to be up too. This type of clever work and exposure!
    Keep up the good works guys I’ve you guys to my personal blogroll.

  24. CharlesDog表示:

    https://viagras.online/# Cheap generic Viagra

  25. GeorgeSep表示:

    Viagra online price: Cheap Sildenafil 100mg – sildenafil over the counter

  26. 3 Ways The Online Shopping Uk Amazon Will Influence Your Life Mike & Ike 4.5 lb

  27. GeorgeSep表示:

    Levitra tablet price: Levitra generic price – Levitra online USA fast

  28. 10 Things You’ve Learned From Kindergarden Which
    Will Help You With Double Glazing Companies Near Me repairing double glazing Windows

  29. The Top Reasons People Succeed At The CSGO Case Battle Sites Industry Cs2 case Opening

發佈留言

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