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,123,799 Responses

  1. The Reasons Assessment Adult Adhd Is Tougher Than You Think
    assessments for adhd In adults

  2. What Subaru Replacement Key Services Uk Experts Want You
    To Be Able To Subaru Car Key Replacement Cost

  3. I all the time emailed this blog post page to all my associates,
    for the reason that if like to read it after that my contacts will too.

    Feel free to surf to my web site :: lastPostAnchor

  4. What’s up to all, the contents present at this site are genuinely awesome
    for people experience, well, keep up the good work fellows.

  5. Quality content is the key to be a focus for the visitors to
    go to see the site, that’s what this site is providing.

  6. HorsePower Brabds Omaha
    2525 N 117tһ Ave #300,
    Omaha,NᎬ 68164, United States
    14029253112
    companies home services

  7. Drstobeese表示:

    target pharmacy lipitor: pharmacy store clipart – fluoxetine online pharmacy

  8. Abrahamwarne表示:

    online pharmacy that sell adipex: pharmacy warfarin counselling – giant grocery store pharmacy

  9. Easydrorbix表示:

    the pharmacy store apopka: quit smoking – cialis online review online pharmacy

  10. Easydrorbix表示:

    zithromax online pharmacy: can you buy viagra at pharmacy – online pharmacy viagra uk

  11. Drstobeese表示:

    clozaril pharmacy registration: pharmacy prices – pharmacy rx one coupon

  12. Je n’était qu’un simple étudiant au baccalauréat.

  13. asiabet表示:

    I think what you said was actually very logical. But, what about this?
    what if you added a little content? I mean, I don’t wish to tell you how to run your blog, but
    suppose you added something that grabbed a person’s attention?
    I mean JS 依照元素出現數量排序 – 馬老師
    雲端研究室 is a little boring. You should glance at Yahoo’s home page and
    watch how they write article headlines to get viewers to open the links.

    You might add a related video or a related pic or two to grab people interested about what you’ve written. In my opinion, it would bring your website a little bit more interesting.

  14. The Top Jaguar Xf Replacement Key It’s What Gurus Do Three Things jaguar f Pace key fob replacement

  15. Easydrorbix表示:

    anti-depressants: mexican online pharmacies – valacyclovir indian pharmacy

  16. Easydrorbix表示:

    lansoprazole pharmacy: u s online pharmacy – first medicine online pharmacy store discount code

  17. Drstobeese表示:

    bitcoin pharmacy online: amoxil online pharmacy – finpecia uk pharmacy

  18. These are truly fantastic ideas in regarding blogging.
    You have touched some fastidious factors here. Any way keep up wrinting.

  19. konteyner表示:

    konteyner modelleri | Makaleniz, iş hayatımda karşılaştığım birçok duruma çözüm sundu. Yazınızı okuduktan sonra konuyu daha iyi anlamaya başladım.

  20. Since Priya has gotten her chili recipe from an real Texan, the apparent query to inquire Sheldon is, “Are there Texans who make a thing they phone ‘chili’ and set beans in it?” Sheldon can only remedy, “Yes.” If so, then Sheldon is hoping to hoodwink the “foreigner” by falsely symbolizing the chili condition in Texas. Drivers who have acquired education or educational methods on human trafficking could be able to determine and support individuals at risk. Checking in all over while trying new positions and styles is key it’ll assist you both equally find out a little bit about what is or just isn’t pleasurable. I figured it wouldn’t make that a lot change, and I could just cook down the sauce a little bit more. Even if you think they’re completely wrong.” –Aurynn Shaw “Don’t make adversaries where by substantial profile, persuasive allies exist if you will not have to.” –Naomi Wu “Take me down to Parallax City in which the again moves gradual and the front moves promptly.” –Fraggle “Some fights are not worth currently being there to have.” –Gray’s Law “Actually inventing or developing matters is at greatest the route to little-time wealth.

  21. 8mph Folding Mobility Scooter Uk: The Secret Life
    Of 8mph Folding Mobility Scooter Uk Can Mobility scooters drive on The road

  22. Medicament information for patients. Effects of Drug Abuse.
    buying generic phenergan without dr prescription
    Actual about drugs. Read information now.

  23. Vayam表示:

    Ahaa, its good dialogue on the topic of this article at
    this place at this website, I have read all that, so at this time me also commenting at this place.

  24. The Three Greatest Moments In Hire Auto Accident Lawyer History Salt lake City auto accident Attorney

  25. 16 Must-Follow Pages On Facebook For Replacement Car Key Cost-Related Businesses Replacement Car Keys With Chips

  26. Easydrorbix表示:

    provigil international pharmacy: pharmacy in artane castle – cialis generic online pharmacy

  27. Who’s The Top Expert In The World On Key Programming Near
    Me? Cut And Program Car Keys

  28. tobrut表示:

    I’m amazed, I must say. Rarely do I encounter a blog that’s both equally educative and entertaining, and let
    me tell you, you’ve hit the nail on the head. The problem is something
    that not enough men and women are speaking intelligently
    about. I am very happy I stumbled across this in my search for
    something regarding this.

    Here is my web-site: tobrut

發佈留言

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