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,197,330 Responses

  1. 10 Replacement Upvc Door Handles Related Projects That Can Stretch
    Your Creativity upvc door blinds [sefaatas.com.tr]

  2. You actually expressed that effectively.

  3. 11 Ways To Completely Sabotage Your Railroad Injuries Attorneys
    railroad injuries Lawsuits

  4. Hello,
    What an good blog!
    Can I scrape this and share it with my group members?

    My channel is about Korean 온리팬즈야동
    If ever your interested, feel free to visit my group and
    check it out.
    Thanks a lot and Keep up the good work!

  5. How Does One Lower My Credit Card Interest Selling Price?
    무직자 3000만원 대출

  6. Upvc Window Repairs Near Me Tools To Improve Your Everyday Lifethe Only Upvc Window Repairs
    Near Me Trick Every Person Should Learn window repairs near me

  7. Child Porn表示:

    Well spoken certainly! !

  8. The 9 Things Your Parents Teach You About ADHD No Medication Adhd No medication

  9. mspeech.kr表示:

    How To Outsmart Your Boss With Adhd Symptoms In Adult Women attention deficit hyperactivity disorder adhd symptoms (mspeech.kr)

  10. betflik 8k表示:

    Hi there to every body, it’s my first pay a visit
    of this webpage; this blog includes awesome
    and actually excellent stuff in support of visitors.

  11. Hi there i am kavin, its my first time to commenting anywhere, when i read this article i
    thought i could also create comment due to this sensible article.

  12. Pills information. What side effects?
    how to get zoloft without a prescription
    Best what you want to know about drugs. Read information here.

  13. Emory表示:

    11 Creative Methods To Write About Double Glazed
    Near Me local double glazing repairs; Emory,

  14. Upvc Door Repairs Near Me Tools To Improve Your Daily Life Upvc Door Repairs Near Me Trick That Everybody Should Know upvc door Repairs near me

  15. Are Adhd Symptoms For Females The Most Effective Thing That
    Ever Was? what Are adhd Symptoms in Adults

  16. Tamera表示:

    The Best Advice You’ll Ever Receive On Peritoneal Mesothelioma Not Caused By Asbestos Tamera

  17. malpractice表示:

    Guide To Malpractice Litigation: The Intermediate Guide To Malpractice Litigation malpractice

  18. betflik 8k表示:

    It’s really a great and helpful piece of info. I’m happy that
    you simply shared this useful info with us.
    Please keep us informed like this. Thank you for sharing.

  19. 9 . What Your Parents Taught You About Upvc Window Repairs Upvc window repairs

  20. Watch Out: How Modular Sectional Sofa Is Taking Over And What Can We Do
    About It http://www.4452346.xyz

  21. 5 Killer Quora Answers On Double Glazed Near Me double glazed near me

  22. There’s definately a great deal to know about this issue.
    I like all the points you made.

  23. betflik 8k表示:

    I think this is one of the so much important information for me.
    And i am satisfied studying your article. However wanna statement on few normal things,
    The site style is great, the articles is truly great :
    D. Excellent process, cheers

  24. Medication information. What side effects?
    cost mesalamine online
    Some information about drug. Get here.

  25. Wright mowers表示:

    Pretty section of content. I just stumbled upon your web
    site and in accession capital to assert that
    I acquire in fact enjoyed account your blog posts. Any way
    I will be subscribing to your feeds and even I achievement you access
    consistently fast.

  26. The 10 Scariest Things About Window Repairs Near Me Window repairs near Me

  27. What’s The Current Job Market For Double Glazing Repairs
    Near Me Professionals? double glazing repairs Near Me

  28. Meds information leaflet. What side effects?
    how can i get azelastine pills
    Best trends of medicament. Get now.

  29. History Of Veterans Disability Law: The History Of
    Veterans Disability Law Veterans Disability lawyer (https://kizkiuz.com/user/matilda90e/)

  30. Meds information leaflet. Drug Class.
    cost of cheap paxil without insurance
    All information about medication. Get here.

發佈留言

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