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,147,224 Responses

  1. Medicament information for patients. Effects of Drug Abuse.
    can i order generic irbesartan
    All information about medicament. Get information now.

  2. You’ll Be Unable To Guess Online Shopping Sites Top 7’s Tricks Online Shopping Sites Top 7

  3. AdrianLoals表示:

    לחצים ודאגות. ולכן עכשיו זה הזמן לעצור ולשים את כל בצד. ואין דרך טובה יותר לעצור את המחשבות והדאגות, מאשר בילוי מענג שכולו התשובה היא פשוטה, עיסוי אירוטי בחולון הוא הפינוק המושלם לגבר. זהו הפינוק שכל גבר צריך וכל גבר רוצה. גברים רבים נהנים מעיסויים נערות ליווי מלאות

  4. What’s The Job Market For Replacement Windows Prices Professionals?
    Replacement Windows Prices

  5. AW8 Official表示:

    I loved as much as you’ll receive carried out right here.
    The sketch is tasteful, your authored subject matter stylish.
    nonetheless, you command get got an edginess over that you wish be delivering
    the following. unwell unquestionably come further formerly again as exactly the same nearly a
    lot often inside case you shield this increase.

  6. Some Of The Most Ingenious Things That Are Happening With Upvc Windows Repair repair Upvc Window

  7. Why Everyone Is Talking About Online Shopping Uk Electronics This Moment Dishwasher Safe Apple Corer

  8. Very good write-up. I absolutely appreciate this site.

    Stick with it!

  9. Slot Updates表示:

    Guide To Slot Updates: The Intermediate Guide In Slot
    Updates Slot Updates

  10. 10 Wrong Answers For Common Truck Accident Attorney Questions Do You Know
    The Right Ones? Truck Accidents

  11. Slot Symbols表示:

    15 Presents For Your Slot Innovations Lover In Your Life
    Slot Symbols

  12. Door Repair表示:

    How Window Repair Luton Was Able To Become The No.1 Trend In Social Media
    Door Repair

  13. firm表示:

    Personal Injury Law Isn’t As Tough As You Think firm

  14. 11 Ways To Totally Defy Your Online Shopping Stores List
    Acrylic Paint Brushes

  15. This is a topic that’s near to my heart… Many thanks!

    Exactly where are your contact details though?

  16. Why Nobody Cares About Railroad Injuries Litigation Railroad Injuries Lawyers

  17. Responsible For The Slot Trends Budget? 10 Terrible Ways To Spend Your Money Slot machine Tips

  18. vimeo.com表示:

    It’s The Ugly Truth About Online Shopping Sites shin guards for young players (vimeo.com)

  19. Chroma 3 Case表示:

    5 Must-Know Hismphash Practices You Need To Know For 2023
    Chroma 3 Case

  20. Diplom_xdKl表示:

    купить диплом гознак http://www.ukr-diplom.ru .

  21. Guide To Upvc Repairs Near Me: The Intermediate Guide On Upvc Repairs Near Me Upvc repairs near me

  22. The 10 Most Scariest Things About Best Slot Machines Multi-Line Slots

  23. Diplom_jlKl表示:

    купить аттестат за 11 класс http://ukr-diplom.ru .

  24. Heya i’m for the first time here. I found this board and I find It truly useful & it
    helped me out a lot. I hope to give something
    back and aid others like you aided me.

  25. Diplom_lzKl表示:

    купить диплом старого образца купить диплом старого образца .

  26. Diplom_oeKl表示:

    купить диплом стоматолога http://www.ukr-diplom.ru .

  27. What’s The Current Job Market For Which Is The
    Best Online Supermarket Professionals Like? Which is the best online supermarket

  28. 10 Things You Learned From Kindergarden They’ll Help You Understand
    Slot Rewards Best payouts slots

發佈留言

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