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,157,637 Responses

  1. How To Solve Issues With SEO Tools seo Tool Software

  2. I like the valuable info you provide in your articles.
    I’ll bookmark your blog and check again here frequently.
    I am quite sure I will learn many new stuff right here!

    Good luck for the next!

  3. Автори доповіді вважають, що саме ці документи допоможуть усунути фактична
    невідповідність між змістом терміна “інформаційне.

    Here is my page https://superkomp.com.ua/yak-vibrati-velosiped/

  4. 10 Things People Hate About Best Online Shopping Websites Uk Smead Legal File Storage

  5. bokep hd表示:

    What’s up Dear, are you truly visiting this web site on a regular basis, if so after that
    you will definitely obtain pleasant know-how.

  6. Abraham表示:

    Heya i’m for the first time here. I found this board and I in finding
    It truly useful & it helped me out much. I am hoping to give one
    thing again and help others like you aided me.

  7. Drugs information. Drug Class.
    how to buy generic thorazine without dr prescription
    Some news about medicine. Read information now.

  8. 8 Tips To Enhance Your Double Glazing Luton Game window replacement luton

  9. 비아그라 데체 약품
    비아그라 대체 약품: ED 치료를 위한 다른 옵션

  10. Many Of The Common Errors People Do With Workers Compensation Legal
    workers’ Compensation lawsuit

  11. Guide To Double Glazing Repairs Near Me: The Intermediate Guide To Double Glazing Repairs Near
    Me double glazing repairs near me; http://scandwap.xtgem.com,

  12. 5 Laws That Anyone Working In Birmingham Windows Should Be Aware
    Of composite doors and windows birmingham

  13. udin777表示:

    This design is wicked! You obviously know how to keep
    a reader entertained. Between your wit and your videos,
    I was almost moved to start my own blog
    (well, almost…HaHa!) Great job. I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

  14. Ten Upvc Window Repairs That Really Help You Live Better
    upvc window repairs

  15. 10 Websites To Aid You Develop Your Knowledge About Birth Injury Attorneys Birth Injury Lawsuits

  16. You’ll Never Guess This Online Shopping Uk Amazon’s Tricks online shopping Uk Amazon

  17. Famous Slots表示:

    What Is The Reason Adding A Key Word To Your Life’s Journey Will Make The Impact Famous Slots

  18. 10 Quick Tips To New Slots Online wild slots (http://dott-comm.com/)

  19. Five Killer Quora Answers To Window Replacement
    Near Me window Replacement Near Me

  20. 7 Secrets About Bunk Beds To Single Beds That Nobody Can Tell You single White bunk Beds

  21. 10 Best Facebook Pages That I’ve Ever Seen. Best Slots Popular Slots
    https://Cse.Google.Com.Mt/,

  22. This post presents clear idea for the new viewers of blogging,
    that really how to do blogging and site-building.

  23. 5 Killer Quora Answers On Shopping Online Uk
    Clothes Shopping Online Uk Clothes

  24. What Is The Reason Generalized Anxiety Disorder Is The
    Best Choice For You? Symptoms Of Anxiety Disorders

  25. Good day I am so glad I found your web site, I really found
    you by mistake, while I was searching on Aol for something else, Nonetheless I am
    here now and would just like to say many thanks for a marvelous post and a all
    round entertaining blog (I also love the theme/design),
    I don’t have time to look over it all at the moment but I have book-marked it and also
    added your RSS feeds, so when I have time I will be back to read a great deal
    more, Please do keep up the excellent job.

  26. 10 Strategies To Build Your Assessing Mental Health Empire dcfs Mental health Assessment

發佈留言

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