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,374,976 Responses

  1. Pedroereva表示:

    mexican online pharmacies prescription drugs: Online Pharmacies in Mexico – mexican rx online

  2. programmed表示:

    The 12 Most Obnoxious Types Of Tweets You Follow programmed

  3. The Next Big Event In The Window Replacement Companies Industry replacement Windows

  4. Don’t Buy Into These “Trends” Concerning Double Glazed Doors Near
    Me double glazed door locks

  5. Julius表示:

    Where Will Upvc Window Repairs Be 1 Year From This Year? upvc window repairs near me (Julius)

  6. JustinCit表示:

    mail order pharmacy india http://indiaph24.store/# Online medicine home delivery
    online shopping pharmacy india

  7. Kareem表示:

    Why Asbestos Lawyer Can Be More Dangerous Than You Thought asbestos compensation (Kareem)

  8. Marcelabelp表示:

    http://canadaph24.pro/# canadian neighbor pharmacy

  9. Delilah表示:

    10 Ways To Create Your Double Glazed Windows Near Me Empire misted double glazing – Delilah,

  10. If some one wants expert view about running a blog afterward i suggest him/her
    to visit this website, Keep up the pleasant work.

  11. The Reasons Online Home Based Work Will Be Everyone’s Desire In 2023 work from home jobs Leicester

  12. Jonathon表示:

    See What Treadmills Best Tricks The Celebs Are Using treadmills best (Jonathon)

  13. Michaelcew表示:

    https://indiaph24.store/# top 10 online pharmacy in india

  14. Pasquale表示:

    15 Things Your Boss Wishes You’d Known About Avon Discontinued
    Perfume avon discontinued perfume (Pasquale)

  15. Rickysem表示:

    mexican mail order pharmacies Online Pharmacies in Mexico mexican online pharmacies prescription drugs

  16. Everything You Need To Learn About Online Job Vacancies Work From
    Home work from home Jobs leicester

  17. web site表示:

    Good day! I just would like to offer you a big thumbs up for the great
    information you have right here on this
    post. I will be returning to your website for more soon.

  18. naga169表示:

    Hi! I’ve been reading your web site for some time now and finally got the courage to go ahead and give you a shout out
    from Austin Tx! Just wanted to tell you keep up the great job!

  19. ladies表示:

    5 Things That Everyone Doesn’t Know Regarding Designer Handbags Large ladies

  20. JustinCit表示:

    top 10 pharmacies in india https://indiaph24.store/# indian pharmacy paypal
    indian pharmacy online

  21. 11 Ways To Completely Revamp Your Get Car Key Cut
    Cutting Car key

  22. 10 Apps That Can Help You Control Your 9kg Washing Machine Cheap Cheapest 9kg Washing machine

  23. Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some time and was hoping maybe
    you would have some experience with something like this.

    Please let me know if you run into anything. I truly
    enjoy reading your blog and I look forward to your new updates.

  24. Responsible For A Cerebral Palsy Attorney Budget? 12 Ways To Spend Your Money cerebral palsy law firm

  25. Are You Responsible For The Upvc Window Repairs Budget?
    12 Ways To Spend Your Money upvc window repairs Near me

  26. Rickysem表示:

    reputable mexican pharmacies online Online Pharmacies in Mexico purple pharmacy mexico price list

  27. There’s certainly a loot to find out about this subject.
    I love all of the points you made.

    Feel free to visit my homepage brazilian wood pills

發佈留言

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