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

  1. Wow that was strange. I just wrote an really long comment but after
    I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say excellent blog!

  2. Three Of The Biggest Catastrophes In Workers Compensation Litigation History workers’ Compensation Attorneys

  3. 5 The 5 Reasons ADHD Adult Treatment Is Actually A Positive Thing adult Adhd Diagnosis and treatment

  4. BomesJoype表示:

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

  5. Slot Sites表示:

    Is Real Money Slots The Most Effective Thing That Ever Was?
    Slot Sites

  6. A Look At The Future What Is The Mesothelioma Compensation Industry Look
    Like In 10 Years? Mesothelioma Lawyers

  7. roof repair表示:

    Have you ever considered writing an e-book or guest authoring on other websites?
    I have a blog based upon on the same information you discuss and would love to have you share some
    stories/information. I know my readers would
    value your work. If you are even remotely interested,
    feel free to shoot me an email.

  8. I was excited to uncover this website. I want to to thank
    you for ones time due to this wonderful read!! I definitely loved every part of it
    and I have you book-marked to check out new things in your site.

    My page – lung nutra lung clear pro

  9. Howdy! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?

    My site – https://maryq906ojd8.westexwiki.com/user

  10. Responsible For A Online Shopping Sites Uk Budget?

    12 Best Ways To Spend Your Money 25 Ft Ethernet Cable

  11. 20 Fun Infographics About Window Repairs Near
    Me Upvc window Repairs near me

  12. Medication information leaflet. Generic Name.
    increasing lisinopril 20 mg
    Some what you want to know about meds. Read information now.

  13. Awesome! Its in fact awesome post, I have got much clear
    idea about from this post.

  14. The 10 Most Scariest Things About 3 Seater Chaise 3 seater Chaise

  15. My programmer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because
    of the costs. But he’s tryiong none the less. I’ve been using WordPress
    on a number of websites for about a year and am worried about switching to another platform.
    I have heard very good things about blogengine.net.
    Is there a way I can transfer all my wordpress content into it?
    Any help would be greatly appreciated!

  16. Hurrah! In the end I got a webpage from where I
    be able to truly take valuable facts regarding my study and knowledge.

  17. Tuyet表示:

    Missing, buckling, and curling shingles.If the roof is young, these damages may
    be a sign that the roof is defective.

  18. 5 Killer Quora Answers To Charity Shop Online Clothes Uk charity shop Online clothes uk

  19. 7 Tips To Make The The Most Of Your Glass Repair Luton glass window repair

  20. auto Accident表示:

    You’ll Be Unable To Guess Auto Accident Lawyers’s Secrets auto Accident

  21. Makayla表示:

    The 9 Things Your Parents Taught You About Slot Strategies progressive slots (Makayla)

  22. Why No One Cares About Slot Games Online Slot Machines

  23. What’s up, constantly i used to check website posts
    here in the early hours in the daylight, because i enjoy to learn more and more.

  24. Fastidious respond in return of this issue with solid arguments and telling the
    whole thing concerning that.

發佈留言

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