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,509,151 Responses

  1. The author is called Roseline. For years she’s been working
    regarding interviewer but she’s always wanted her business.

    As a man what he really likes is doing interior design but he doesn’t
    offer the time as of late. My wife and I live in New Shirt.

  2. KevinSounc表示:

    http://slotsiteleri.bid/# slot siteleri guvenilir

  3. Indisputable Proof You Need Coffee Beans Near Me coffee Bean coffee

  4. Howdy! Would you mind if I share your blog with my twitter group?
    There’s a lot of people that I think would really appreciate your content.

    Please let me know. Many thanks

  5. Williams表示:

    15 Things You Didn’t Know About Leather Couch reupholster leather couch [Williams]

  6. Darrelherse表示:

    slot siteleri guvenilir: deneme bonusu veren slot siteleri – en guvenilir slot siteleri

  7. Esmeralda表示:

    The 9 Things Your Parents Taught You About Bi Fold Door Repair Near Me bi fold door repair (Esmeralda)

  8. memek表示:

    It’s wonderful that you are getting ideas from this article as well as from our dialogue made at this place.

  9. Hmm is anyone else experiencing problems with the images
    on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog.
    Any responses would be greatly appreciated.

  10. Joanne表示:

    Bed Settee For Sale Tools To Make Your Everyday Lifethe Only Bed Settee For Sale Trick That Every
    Person Must Be Able To bed settee for sale (Joanne)

  11. The People Nearest To ADHD Online Test Tell You Some Big Secrets
    Female Adhd Test Free – Genebiotech.Co.Kr,

  12. treadmills uk表示:

    5 Killer Quora Answers On Treadmills UK treadmills uk

  13. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza 90 tl

  14. KevinSounc表示:

    http://slotsiteleri.bid/# slot oyun siteleri

  15. 5 Killer Quora Answers To Test For ADHD In Adults test for adhd
    in adults (https://ordermall.Co.kr)

  16. A Look At The Ugly Real Truth Of Best Accident
    Lawyers accident Lawyers Near Me

  17. The Three Greatest Moments In Oil Filled Wall Mounted Radiators
    History Oil filled electric Radiators

  18. Katie表示:

    Look at my web page … Explore Daycares Locations (Katie)

  19. Salutations, companion reader. I must applaud the author for their discerning and eloquently-crafted blog
    post. The subject was both insightful and introspective, leaving me with
    a more nuanced understanding of the issue
    at hand.

    I would desire to extend an recommendation to become a member of the
    renowned PUSSY888 organization. This space offers a panorama of fun and stimulation, suiting those who cherish
    the more elegant things in reality. I urge you to venture
    into the multifaceted selections and immerse yourself in the exciting explorations that summon you.
    Your participation would be extremely welcomed, and I anticipate with
    enthusiasm the opportunity to engage with you more intimately within this distinguished web-based landscape

    Also visit my web-site; online casino player behavior analytics

  20. services表示:

    14 Clever Ways To Spend On Leftover Nissan Juke Key Replacement Budget services

  21. I love your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do
    it for you? Plz respond as I’m looking to create my
    own blog and would like to find out where u got this from.
    appreciate it

  22. 9 Things Your Parents Taught You About Bifold Door Repair bifold door Repair

  23. Five Lessons You Can Learn From Mesothelioma Law Firm Mesothelioma Lawyer

  24. evisa vietnam表示:

    I like looking through an article that can make men and women think.
    Also, thank you for allowing for me to comment!

  25. When someone writes an piece of writing he/she keeps the image of a user in his/her
    mind that how a user can know it. Therefore that’s why this article is outstdanding.
    Thanks!

  26. 10 Things You Learned In Kindergarden That’ll Help You With Treadmill With Incline
    Uk Under Bed treadmill with incline – big.lordfilm-s.club,

  27. Darrelherse表示:

    en yeni slot siteleri: guvenilir slot siteleri – slot kumar siteleri

  28. The 9 Things Your Parents Teach You About Lost Lexus Key Fob lost Lexus key

  29. Why Is There All This Fuss About Male Depression Symptoms?
    major Depressive symptoms

  30. Sabrina表示:

    Hello there, just became aware of your blog through Google, and found that it’s really informative.

    I’m gonna watch out for brussels. I’ll be grateful if you continue
    this in future. Lots of people will be benefited from your writing.
    Cheers!

發佈留言

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