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,155,127 Responses

  1. You’ll Be Unable To Guess Online Clothes Shopping Websites Uk’s Tricks online clothes Shopping Websites uk (validator.W3.org)

  2. Upriserz.Link表示:

    Five Killer Quora Answers To Federal Railroad Federal Railroad (Upriserz.Link)

  3. 11 Strategies To Refresh Your Erb’s Palsy Claim Erb’s Palsy Law Firm

  4. Medicament information for patients. Generic Name.
    get cheap azelastine pills
    Everything trends of medicine. Get information now.

  5. popular Slots表示:

    17 Signs You’re Working With Best Slots popular Slots

  6. Marketing表示:

    15 Search Optimisation Benefits That Everyone Should Know Marketing

  7. It’s nearly impossible to find experienced people in this particular topic,
    however, you seem like you know what you’re talking about!
    Thanks

  8. Hi there, I log on to your new stuff daily. Your story-telling style is awesome, keep doing what you’re doing!

  9. A Productive Rant Concerning Dangerous Drugs Law Firm Dangerous Drugs Law Firms

  10. In recent years, the ketogenic diet has gained popularity for its potential health benefits, including weight loss, improved energy levels, and better mental focus.

    My site – http://dsmet.net/bbs/board.php?bo_table=free&wr_id=171252

  11. peatix.com表示:

    The Reasons You Should Experience New Slots Online At The Very Least
    Once In Your Lifetime reputable slots (peatix.com)

  12. 10 Facts About Wall Mount Fireplaces That Will Instantly Get You Into A Great Mood lynnbolvin.top

  13. Hi to every one, the contents existing at this site are really awesome for people experience, well, keep up the nice work
    fellows.

  14. Asbestos Lawyer
    is made up of a collection of minerals that are naturally
    occurring. They are resistant to rust, durable and fire retardant.
    They were previously widely used in the construction industry, but they have been removed due to health risks.

  15. rcin.Org.pl表示:

    10 Double Size Mattress-Friendly Habits To Be Healthy double Mattress bed (rcin.Org.pl)

  16. Drugs prescribing information. Short-Term Effects.
    where to get atarax prices
    Best trends of drug. Get information here.

  17. The No. One Question That Everyone Working In Online Shopping Sites Should Be Able To Answer
    Online shopping sites for dress

  18. What’s The Job Market For Uk Online Phone Shopping Sites Professionals?
    Uk Online Phone Shopping Sites

  19. Woah! I’m really digging the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s very hard how to do local SEO get that “perfect balance” between user friendliness and visual appearance.
    I must say you’ve done a amazing job with this. Additionally, the blog loads
    very quick for me on Firefox. Excellent Blog! – Creating
    A Consistent Local SEO-http://news.worldsharemarketlive.com/story/446185/boost-business-visibility-with-local-seo-dont-be-invisible-to-potential-customers-for-online-searches.html

  20. BennieBof表示:

    Jude Bellingham https://jude-bellingham.prostoprosport-br.com Futebolista ingles, meio-campista do clube espanhol Real Madrid e do Selecao da Inglaterra. Em abril de 2024, ele ganhou o premio Breakthrough of the Year do Laureus World Sports Awards. Ele se tornou o primeiro jogador de futebol a recebe-lo.

  21. Couches Sale表示:

    9 Things Your Parents Teach You About Couches Sale Couches Sale

  22. The 10 Scariest Things About Green Power Mobility Scooter Power mobility scooter

  23. doc.hypra.fr表示:

    20 Important Questions To ASK ABOUT Cerebral Palsy Claim Prior To Purchasing Cerebral Palsy Claim cerebral
    palsy law firm (doc.hypra.fr)

  24. Medicines information. What side effects?
    where can i buy zithromax without rx
    Some what you want to know about medication. Get here.

  25. Zack Foxworth表示:

    How Much Can Small American Fridge Freezer Experts Make? Zack Foxworth

  1. 2024-07-01

    metformin dosage when to take

    metformin dosage when to take

發佈留言

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