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,146,906 Responses

  1. Three Reasons To Identify Why Your Door Fitter Leeds Isn’t Working (And How To Fix It) leeds double glazing Repairs

  2. Guide To Shop Online Uk Women’s Fashion: The Intermediate
    Guide On Shop Online Uk Women’s Fashion shop online uk women’s fashion (m.ayoueryi.com)

  3. ElmerWes表示:

    п»їkamagra kamagra.win Kamagra Oral Jelly

  4. hey there and thank you for your info on explainer Videos ? I have certainly picked up something new from right here. I did however expertise a few technical issues using this web site, as I experienced to reload the web site many times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but slow loading instances times will very frequently affect your placement in google and can damage your high quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for much more of your respective intriguing content. Ensure that you update this again soon.. https://local.gocommercially.com/business/explainer-video-company-india

  5. CharlesDog表示:

    https://cialist.pro/# Buy Cialis online

  6. ElmerWes表示:

    Kamagra 100mg price kamagra pills Kamagra tablets

  7. Designer bags表示:

    What NOT To Do Within The Designer Handbags Red Industry
    Designer bags

  8. Nice post. I was checking continuously this explainer video blog and I am impressed! Very useful information particularly the last part 🙂 I care for such info a lot. I was seeking this particular information for a very long time. Thank you and best of luck. https://controlc.Com/54d90949

  9. JamesNow表示:

    Cenforce 150 mg online: cenforce for sale – order cenforce

  10. Mickie表示:

    The 10 Scariest Things About Cerebral Palsy Attorneys cerebral palsy attorneys (Mickie)

  11. Where Are You Going To Find Smeg Fridge Freezers Be 1 Year From Today?
    Best integrated fridge freezer 2022

  12. Washer And Dryer In One Machine Explained In Fewer Than 140 Characters washer And dryer In one machine

  13. JamesNow表示:

    Levitra online USA fast: levitrav.store – Buy Vardenafil 20mg

  14. Are You Responsible For The Uk Online Grocery Shopping Sites
    Budget? 12 Top Ways To Spend Your Money photography Lighting kit

  15. Yakin777表示:

    I don’t know if it’s just me or if everybody else experiencing issues with your blog.
    It appears as though some of the text within your posts
    are running off the screen. Can someone else please comment and let
    me know if this is happening to them too? This may be a problem with my web browser because
    I’ve had this happen before. Kudos

  16. It is explainer video company india perfect time to make some plans for the expaliner videos and it is time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or suggestions. Maybe you can write next articles referring to this article. I wish to read even more things about it! https://www.nextbizthing.com/business-support-20-supplies-20-services/explainer-video-company-india

  17. Seven Reasons To Explain Why Cerebral Palsy
    Lawyers Is Important cerebral palsy attorneys

  18. ElmerWes表示:

    Viagra online price Cheap Viagra 100mg Cheap Sildenafil 100mg

  19. Titration ADHD Tools To Ease Your Daily Lifethe One Titration ADHD Trick Every Person Should Be Able To titration adhd

  20. shorl.com表示:

    The Reason Glass Repair Leeds Is So Beneficial When COVID-19 Is In Session door repairs leeds – shorl.com

  21. GeorgeSep表示:

    sildenafil oral jelly 100mg kamagra: п»їkamagra – Kamagra 100mg price

  22. 9 Lessons Your Parents Teach You About Upvc Window Repairs Near Me window repairs Near me

  23. Great post. I used to be checking continuously this blog and I’m inspired!
    Very helpful info specially the ultimate part :
    ) I take care of such information much. I was seeking this particular information for a very long time.
    Thank you and good luck.

  24. Hi from explainer video company india, just became alert to your blog through Google Explainer video Company india, and found that it’s truly informative. I?m gonna watch out for brussels. I will appreciate if you continue this in future. Numerous people will be benefited from your writing. Cheers! https://www.Finditangeles.Com/services/explainer-video-company-india

  25. Five Killer Quora Answers On Designer Handbags Uk Sale designer handbags uk Sale

  26. CharlesDog表示:

    https://cialist.pro/# Cialis 20mg price in USA

  27. The 10 Most Terrifying Things About List Of Online Shopping
    Sites Uk list of Online shopping sites uk

  28. CharlesDog表示:

    https://cenforce.pro/# cenforce.pro

發佈留言

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