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,528 Responses

  1. ElmerWes表示:

    viagra canada viagras.online Viagra generic over the counter

  2. It’s difficult to find well-informed people on this topic, however, you seem
    like you know what you’re talking about! Thanks

    Look at my homepage: lounge chair for sale

  3. Door Repair表示:

    A Step-By Step Guide To Selecting Your Misted Double Glazing Repairs Door Repair

  4. Five Mesothelioma Settlement Projects For Any Budget mesothelioma Lawsuit

  5. Replace Mini Key Fob Techniques To Simplify Your Everyday Lifethe Only Replace Mini Key Fob Trick That
    Should Be Used By Everyone Learn Replace Mini Key

  6. Delia表示:

    The 10 Most Scariest Things About Double Glazing Repairs Near Me double glazing repairs (Delia)

  7. Tony表示:

    You’ll Be Unable To Guess Window Replacement Near Me’s Tricks window replacement near me [Tony]

  8. GeorgeSep表示:

    Cenforce 100mg tablets for sale: cenforce for sale – cenforce.pro

  9. This article is in fact a pleasant one it
    helps new internet users, who are wishing for blogging.

  10. Casino Slots表示:

    Five Killer Quora Answers To Free Casino Slots Casino Slots

  11. Private ADHD Assessment Belfast 101 Your
    Ultimate Guide For Beginners book private adhd Assessment

  12. JamesNow表示:

    Generic Levitra 20mg: Buy generic Levitra online – Vardenafil price

  13. Mesothelioma Lawyer Commercial Explained In Less Than 140 Characters mesothelioma lawyer Near me

  14. CharlesDog表示:

    https://levitrav.store/# п»їLevitra price

  15. CharlesDog表示:

    http://levitrav.store/# Buy Levitra 20mg online

  16. Take A Look At One Of The Upvc Windows Repair Industry’s Steve Jobs Of The
    Upvc Windows Repair Industry repair upvc window; https://Olderworkers.com.au/,

  17. Vimeo表示:

    Why You’re Failing At Railroad Injuries Legal Vimeo

  18. GeorgeSep表示:

    Viagra online price: Cheap Viagra 100mg – generic sildenafil

  19. Omer表示:

    5 Killer Quora Answers To Repair Misted Double Glazing Near Me double glazing near me – Omer,

  20. You’ll Be Unable To Guess Car Replacement Keys’s Tricks Car replacement keys

  21. Gabrielle表示:

    Why No One Cares About Windows Leeds leeds double glazing repairs; Gabrielle,

  22. Guide To Double Glazing Repairs Near Me: The Intermediate
    Guide In Double Glazing Repairs Near Me double glazing repairs Near me

  23. The Ultimate Glossary Of Terms About Personal Injury
    Compensation springfield personal injury Lawsuit

  24. Vimeo.com表示:

    Can 18 Wheeler Wreck Lawyer Ever Rule The World?

    Vimeo.com

  25. WillieRully表示:

    We bieden tijdelijke telefoonnummers in tientallen landen over de hele wereld, met duizenden nummers om uit te kiezen telegram zonderВ telefoonnummer

  26. How Narwal Robot Vacuum Became The Hottest Trend Of 2023
    Self Vacuum Robot

  27. DavidOwelo表示:

    https://cialist.pro/# cialis for sale

  28. JamesNow表示:

    Kamagra 100mg: kamagra oral jelly – Kamagra 100mg price

發佈留言

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