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,232,988 Responses

  1. CharlesDog表示:

    http://viagras.online/# Viagra tablet online

  2. JamesNow表示:

    viagra without prescription: Buy Viagra online – viagra without prescription

  3. Ahaa, its pleasant dialogue concerning this post here at this webpage, I have read
    all that, so at this time me also commenting
    here.

  4. CasinoYYY表示:

    Join the excitement at[url=https://yyy-casinotai.com] YYY Casino[/url] and enjoy a world-class gaming experience from the comfort of your own home. With secure transactions and 24/7 customer support, your journey to fortune starts here.

  5. window Repair表示:

    You’ll Be Unable To Guess Window Repair Near Me’s Secrets window Repair

  6. Fantastic beat ! I would like to apprentice while you amend your site, how can i subscribe for a blog web site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea

  7. JamesNow表示:

    Kamagra tablets: buy Kamagra – buy Kamagra

  8. 15 Startling Facts About Prada Handbags Nylon You’ve Never Known Prada handbag black

  9. Mega888, a epithet that has become synonymous with the world of online
    gaming, has revolutionized the way we explore entertainment and leisure.
    This state-of-the-art platform has not only captivated
    the hearts and minds of gaming enthusiasts but has also paved the way for a
    bold future of digital amusement.

    At the epicenter of Mega888’s dominance lies
    its firm conviction to providing an matchless gaming odyssey .

    Displaying a mammoth range of exhilarating games, from
    timeless slot machines to the most recent in virtual reality
    breakthroughs , Mega888 addresses the assorted desires of its thriving user base .

    The platform’s straightforward structure
    and effortless design render it easy for both experienced gamers and novices to immerse
    themselves in the simulated world of gaming.

    One of the fundamental attributes that distinguishes Mega888
    apart from the rest is its allegiance to safety . Integrating the latest encryption approaches and upholding
    the most demanding industry regulations , the platform shields that every exchange and gameplay endeavor is safe .

    This devotion to protecting its clients’ security
    has achieved Mega888 the trust and unwavering support of customers
    globally .

    Additionally , Mega888 has recognized the importance of
    fostering a vibrant and participatory community.

    By way of its diverse outreach strategies , interactive features ,
    and social media exposure, the platform drives users to
    bond with one another , share their stories , and nurture permanent
    links.

    This ambiance of solidarity not only elevates the overall
    gaming venture but also cultivates a culture of
    solidarity and backing within Mega888’s loyal
    user base.

    The repercussion of Mega888 exceeds vastly outside of
    the arena of online gaming. In the capacity of a visionary in the
    sector , the platform has likewise played a crucial
    part in determining the future of virtual recreation .

    Its cutting-edge perspective to experience design , its
    integration of burgeoning breakthroughs ,
    and its unyielding devotion to customer happiness have set innovative
    targets for the sector to aspire to .

    In in the final analysis, Mega888 has definitively created an enduring footprint on the realm of virtual gaming.
    By way of its firm devotion to leadership, its commitment to safety and even-handedness , and its potential
    to nurture a energetic clientele , Mega888 has
    strengthened its rank as a true leader in the technological leisure field .

    As the landscape transforms, it is obvious that Mega888 will sustain its
    at the helm , driving the horizons of the conceivable
    and mesmerizing the imaginations of entertainment devotees
    throughout the globe .

    Also visit my homepage online casino search engine optimization

  10. vimeo.com表示:

    10 Quick Tips About Uk Women’s Online Shopping Websites Lily Sugar
    N Cream Yarn (vimeo.com)

  11. CharlesDog表示:

    http://kamagra.win/# buy kamagra online usa

  12. vimeo.com表示:

    20 Reasons To Believe Amazon Online Grocery Shopping Uk Cannot Be Forgotten Compatible
    Stand Mixer Cover (vimeo.com)

  13. Skin So Soft表示:

    How You Can Use A Weekly Avon Skin So Soft Dry Oil Spray How To Use Project Can Change Your Life Skin So Soft

  14. What’s The Current Job Market For Double Glazed Window Repairs Professionals?
    Double Glazed Window Repairs (Lovewiki.Faith)

  15. CharlesDog表示:

    http://cenforce.pro/# Cenforce 150 mg online

  16. I was suggested this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks!

  17. You’ll Never Guess This Window Repair Near Me’s Tricks window repair near Me

  18. The Most Effective Reasons For People To Succeed Within The
    Injury Attorney Industry Dixon Injury Lawsuit

  19. 15 Startling Facts About Repair Window You Didn’t Know upvc door repairs near me

  20. Serbu4d Slot表示:

    Hey! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no back up.
    Do you have any methods to prevent hackers?

  21. 10 Simple Steps To Start The Business Of Your Dream Window Repair Near Business window repair near Me

  22. Lorena表示:

    The Most Important Reasons That People Succeed In The Avon Rep Login Mk Industry avon rep login mk; Lorena,

  23. I’m extremely impressed with your writing skills as well as with the layout on your weblog.
    Is this a paid theme or did you customize it yourself?
    Either way keep up the nice quality writing, it’s rare to see a nice blog like this one today.

  24. What’s The Job Market For Nespresso Machine White
    Professionals Like? nespresso machine White (Wifidb.science)

  25. Emory表示:

    What’s The Current Job Market For Multi Fuel Stoves For Sale Professionals Like?
    small multi fuel stoves for sale (Emory)

  26. 10 Facts About Slot Rtp That Will Instantly Put You In An Optimistic Mood Top Winning Slots

  27. 10 Online Home Based Work Tricks Experts Recommend work from home jobs bristol

  28. 5 Killer Quora Answers On Designer Handbags Uk Sale Designer Handbags Uk Sale (http://0553721256.Ussoft.Kr/)

  29. Sophie表示:

    You’ll Never Guess This Double Glaze Repair Near Me’s Tricks double glaze repair near me, Sophie,

  30. ElmerWes表示:

    super kamagra kamagra.win sildenafil oral jelly 100mg kamagra

發佈留言

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