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,154,615 Responses

  1. Stewart表示:

    10 Things You Learned In Kindergarden That’ll Help You With Repairs To Upvc Windows
    upvc window repair (Stewart)

  2. The 10 Most Terrifying Things About Auto Accident Attorneys auto Accident Attorneys

  3. What Experts On Cerebral Palsy Lawsuit Want You To Know?

    cerebral palsy lawyers

  4. 20 Fun Details About Online Shopping Uk Discount
    Shopping online

  5. kaymell.uk表示:

    What Will Slot Be Like In 100 Years? kaymell.uk

  6. 15 Shocking Facts About Railroad Injuries Lawyer That
    You Didn’t Know About railroad injuries law Firms

  7. Angels Bail Bonds Costa Mesa
    769 Baker St,
    Coosta Mesa, ⅭA 92626, United Statеs
    what is bail bond business

  8. Five Killer Quora Answers On Medication For Autism And ADHD medication for autism And adhd

  9. Nexia789表示:

    I was recommended this website via my cousin. I am not positive whether this
    submit is written by means of him as no one else recognize such specific about my trouble.
    You are wonderful! Thank you!

  10. Guide To Uk Online Shopping Sites Like Amazon: The Intermediate Guide The
    Steps To Uk Online Shopping Sites Like Amazon online Shopping sites

  11. See What Online Charity Shop Uk Clothes Tricks The Celebs Are Utilizing online charity Shop Uk clothes

  12. 10 No-Fuss Methods To Figuring Out Your Boat Accident Attorneys boat accident law firm

  13. Edythe表示:

    The 10 Scariest Things About Affordable Local SEO Services Near Me local seo services near me (Edythe)

  14. What’s The Job Market For Double Glazed Window Repairs Professionals?
    Double Glazed Window Repairs (https://Tujuan.Grogol.Us/Go/AHR0CHM6Ly90ZW1WYXN0ZS5Jb20VYUpkSWI2Ym9VWHU)

  15. Do Not Buy Into These “Trends” Concerning Upvc Window Repairs window repairs near me

  16. Five Killer Quora Answers To Dangerous Drugs Law Firm dangerous drugs law firm

  17. Howdy! Someone in my Myspace group shared this site with us so I came
    to take a look. I’m definitely enjoying the information. I’m book-marking and will be
    tweeting this to my followers! Superb blog and outstanding
    design.

  18. slot strategy表示:

    10 Quick Tips About Slot Strategies slot strategy

  19. There are four primary forms of cash advances — bank card cash
    advances, payday loans, installment loans, and merchant cash advances.

  20. Does Technology Make Auto Accident Law Better Or Worse?
    auto accident lawsuit

  21. 10 Quick Tips About Dangerous Drugs Lawyer Dangerous drugs attorney

  22. Why You Should Concentrate On Improving Slot Updates Low Variance slots

  23. Dr. Aamir Safdar is Board Certified in Adult Psychiatry as well as Child and Adolescent
    Psychiatry.

  24. Мы заботимся о том, чтобы вы испытали идеальное вращение с нашим бездепозитным бонусом на
    бесплатное вращение и приветственными бонусными кодами R7.

  25. How Private Psychiatrist North London Has Transformed My Life The Better london Private Psychiatrist

  26. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://selfpas.it/qjgmx of type and scrambled it to make a type specimen book.

發佈留言

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