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...

916,122 Responses

  1. popular表示:

    The 10 Most Popular Pinterest Profiles To Keep Track Of Designer Handbags For Work
    popular

  2. JamesNow表示:

    buy kamagra online usa: kamagra.win – buy Kamagra

  3. CharlesDog表示:

    https://levitrav.store/# Buy generic Levitra online

  4. An outstanding share! I have just forwarded this onto a co-worker who has been conducting a little research on this.
    And he actually bought me breakfast simply because I found it for him…

    lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanks for spending time to discuss this issue here
    on your internet site.

    My blog post :: 토토사이트순위

  5. designer表示:

    What Is Tommy Hilfiger Bookbag And Why Is Everyone Talking About It?
    designer

  6. DavidOwelo表示:

    http://kamagra.win/# Kamagra tablets

  7. CharlesDog表示:

    https://kamagra.win/# Kamagra Oral Jelly

  8. What Experts From The Field Of Windows Repairs Want You To
    Know? window Repairs

  9. ElmerWes表示:

    Cialis 20mg price in USA buy cialis online Buy Tadalafil 10mg

  10. 5 Common Myths About Work Jobs From Home You
    Should Avoid work jobs from home

  11. 14 Businesses Are Doing A Fantastic Job At Double Glazing
    In Leeds Repairs To Double Glazed Windows

  12. How To Tell If You’re At The Right Level To Go After Local Window Repair window doctor [http://www.huenhue.net]

  13. GeorgeSep表示:

    Sildenafil 100mg price: Buy Viagra online cheap – order viagra

  14. Don’t Believe These “Trends” Concerning Vehicle Lock Repair Car security system Repair

  15. Do Not Buy Into These “Trends” Concerning Audi Replacement Key replacement key Audi

  16. I ⅼove what you gսys are usually up too. This kiind of clever work
    and exposurе! Keep up the excellent works guys I’ve addsd you guys tо mʏ blogroll. https://Netcallvoip.com/wiki/index.php/Pagar_Beton_Lenggek_Bandung_Precast_Concrete

  17. Rodrick表示:

    10 Quick Tips About Automotive Locksmiths Near Me automotive locksmiths near me; Rodrick,

  18. GeorgeSep表示:

    cheapest cenforce: Cenforce 100mg tablets for sale – Cenforce 100mg tablets for sale

  19. JamesNow表示:

    Cialis 20mg price in USA: Cialis 20mg price in USA – Cialis without a doctor prescription

  20. Julio表示:

    See What Where To Buy Electronics Online Tricks The Celebs Are Making Use
    Of where to buy electronics online [Julio]

  21. Five Lessons You Can Learn From Jobs Working Remotely work from home jobs bradford

  22. “The Car Accident Attorney Awards: The Most, Worst, And Weirdest Things We’ve Seen car accident law firms

  23. 10 Facts About Upvc Window Repairs That Insists On Putting You In A
    Good Mood upvc Window Repairs near me

  24. TylerPhona表示:

    https://viagras.online/# Generic Viagra online

  25. Microsoft Me表示:

    you are truly a just right webmaster. The web site loading pace
    is incredible. It sort of feels that you’re doing any
    distinctive trick. Also, The contents are masterpiece.

    you have done a excellent activity in this matter!

  26. replacing表示:

    12 Facts About Double Glazing Windows Repairs To Make You Look Smart Around Other People replacing

  27. upvc Repairs表示:

    7 Things You Didn’t Know About Window Repairs upvc Repairs

發佈留言

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