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,518,964 Responses

  1. Your method of telling all in this paragraph is in fact fastidious, all
    be capable of easily know it, Thanks a lot.

  2. I got this website from my pal who informed me about
    this website and now this time I am visiting this website and reading very informative
    content at this place.

  3. Richelle表示:

    See What Toto Online Terbaik Tricks The Celebs Are Utilizing toto online terbaik (Richelle)

  4. Terrain Buggy表示:

    The Most Worst Nightmare Concerning All-Terrain Stroller Uk Relived
    Terrain Buggy

  5. xoilacvac.cc表示:

    Kudos! Great information.

  6. Hey there! Do you know if they make any plugins to help with SEO?

    I’m trying to get my blog to rank for some targeted keywords
    but I’m not seeing very good success. If you know of
    any please share. Kudos!

  7. Saved as a favorite, I love your site!

  8. Pretty great post. I just stumbled upon your blog and wanted to
    mention that I’ve really enjoyed browsing your blog posts.
    In any case I’ll be subscribing for your feed and I hope you
    write once more very soon!

  9. How To Save Money On Electric Treadmill Vs Manual electric Treadmill under 200

  10. You actually revealed this terrifically!

  11. mobile game表示:

    Whoa! This blog looks just like my old one! It’s on a totally different topic but it has pretty much the same
    layout and design. Wonderful choice of colors!

  12. What’s The Current Job Market For Asbestos Cancer Lawyer
    Mesothelioma Settlement Professionals? Lawyer Mesothelioma

  13. Nine Things That Your Parent Teach You About Treadmills
    Home treadmills Home

  14. bafkor.com表示:

    10 Things That Your Family Taught You About Dripper Coffee Maker dripper Coffee maker (bafkor.com)

  15. Adelaida表示:

    What’s The Job Market For Fresh Arabica Coffee Beans Professionals
    Like? fresh arabica coffee beans (Adelaida)

  16. The All Terrain Prams Case Study You’ll Never Forget Terrain pushchairs

  17. What’s Everyone Talking About Infant Car Seat Best Right Now top Rated Baby car seats

  18. Hi, i read your blog from time to time and i own a similar one and i
    was just wondering if you get a lot of spam remarks?

    If so how do you reduce it, any plugin or anything
    you can recommend? I get so much lately it’s driving me crazy so any
    support is very much appreciated.

  19. click here表示:

    Useful information. Lucky me I discovered your site by accident, and I am surprised why this accident
    didn’t took place in advance! I bookmarked it.

  20. Janis表示:

    Hi there, i read your blog occasionally and i own a
    similar one and i was just wondering if you get a lot of spam feedback?
    If so how do you protect against it, any plugin or anything you can recommend?
    I get so much lately it’s driving me mad so any support is
    very much appreciated.

  21. 20 Fun Details About Test For ADHD In Adults adhd
    for adults test – shinhanact.Com

  22. Good post! We will be linking to this particularly great
    article on our site. Keep up the good writing.

    my site … laundry chute closer

  23. slot表示:

    The other day, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a 25 foot drop, just so she can be a youtube
    sensation. My iPad is now destroyed and she has 83 views.
    I know this is completely off topic but I had to share it with someone!

  24. “27 Reasons Why Celebrities Love This Product”
    먹튀검증

  25. Hi there very nice website!! Guy .. Excellent ..
    Wonderful .. I’ll bookmark your blog and take the feeds also?
    I am satisfied to seek out so many helpful information right
    here within the put up, we need work out more strategies on this regard,
    thank you for sharing. . . . . .

  26. Situs 4D表示:

    Guide To Situs 4d: The Intermediate Guide Towards Situs 4d Situs 4D

  27. scatter hitam表示:

    I think that what you posted was very reasonable. However, think about this, suppose you added a little
    content? I am not saying your content isn’t solid.,
    but suppose you added a title that makes people desire more?

    I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is kinda boring.
    You might look at Yahoo’s front page and note how they create news headlines to get people interested.
    You might add a related video or a picture
    or two to get people excited about what you’ve got
    to say. Just my opinion, it could make your website a little bit more interesting.

  28. bokep jepang表示:

    Hey would you mind sharing which blog platform you’re working
    with? I’m going to start my own blog in the near future but
    I’m having a hard time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking for something unique.

    P.S My apologies for getting off-topic but I had to ask!

  29. I’m impressed, I must say. Seldom do I encounter a blog that’s both educative and interesting, and without a doubt, you’ve hit the nail on the head.
    The problem is something which not enough men and women are speaking intelligently
    about. I’m very happy that I came across this in my search for something regarding this.

發佈留言

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