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

  1. 15 Incredible Stats About Workers Compensation Legal Workers’ compensation lawsuits

  2. winning slots表示:

    You’ll Be Unable To Guess Winning Slots’s Tricks
    winning slots

  3. You’ll Never Guess This Online Clothes Shopping Websites Uk’s
    Tricks Online Clothes Shopping Websites Uk

  4. Childrens Bunk Beds Tools To Improve Your Daily Life Childrens Bunk Beds Technique Every Person Needs To Learn Childrens Bunk Bed

  5. Slot Sites表示:

    9 Things Your Parents Teach You About Slot Sites Slot Sites

  6. The 10 Scariest Things About Blown Double Glazing Repairs Near
    Me double glazing repairs Near me

  7. 인도 비아그라
    인도의 비아그라: 사용을 둘러싼 법적 및 윤리적 고려 사항

  8. TAMPUNG99表示:

    Howdy this is kinda of off topic but I was wondering if
    blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding expertise so I wanted to get guidance
    from someone with experience. Any help would be greatly appreciated!

  9. LOSE MONEY表示:

    Quality articles is the secret to be a focus for the users to pay a visit the web page,
    that’s what this site is providing.

  10. You’ll Be Unable To Guess Online Shopping Sites
    Top 7’s Tricks Online Shopping Sites Top 7

  11. arlennizo表示:

    The Evolution Of Car Boot Scooter arlennizo

  12. Asbestos case表示:

    An Asbestos case lawyer can assist victims file a lawsuit against their employers
    for exposure to the harmful mineral. A lawsuit could help
    victims obtain the cost of medical bills in the past, lost wages, and pain and suffering.

  13. Guide To Online Shopping Uk Discount: The Intermediate Guide The Steps To Online Shopping Uk Discount Online Shopping Uk Discount

  14. 9 Things Your Parents Taught You About Double Glazed Windows
    Near Me double glazed windows near Me

  15. zzb.bz表示:

    Why We Why We Slot Gaming Experience (And You Should Also!) slot symbols
    (zzb.bz)

  16. Guide To Upvc Repairs Near Me: The Intermediate Guide To Upvc Repairs Near Me upvc repairs near Me

  17. What’s The Job Market For SEO Company In London UK Professionals?

    seo company in london Uk

  18. Geraldo表示:

    5 Killer Quora Answers On Cheap Online Electronics Shopping Uk cheap online electronics shopping uk, Geraldo,

  19. Double Glazing Companies Near Me Tools To Streamline Your Daily Life Double Glazing Companies
    Near Me Trick That Every Person Should Know double glazing companies near me

  20. The 10 Scariest Things About Linkedin Content Marketing Linkedin Content Marketing

  21. Michaelamore表示:

    Expat Jobs

  22. 11 Ways To Completely Revamp Your Sofa And Couch Sofas vs Couch

  23. The 10 Most Terrifying Things About Uk Online Shopping Sites For Electronics uk online shopping Sites for electronics

  24. Caitlyn表示:

    8 Tips For Boosting Your Buying Online From Uk To Ireland
    Game online shop (Caitlyn)

  25. Gabriela表示:

    Best Seo Backlink Software: The Ugly Facts About Best
    Seo Backlink Software best backlinking software (Gabriela)

  26. See What ADHD Adults Test Tricks The Celebs Are
    Utilizing Adhd adults Test

  27. Thanks for sharing your thoughts on fitspresso reviews reddit.
    Regards

  28. Vance表示:

    3 Reasons You’re L Shaped Bunk Beds Is Broken (And How
    To Fix It) l shaped toddler beds (Vance)

  29. Tracee表示:

    The very best location for installation remains in an open area that
    obtains great airflow.

發佈留言

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