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,143,587 Responses

  1. Seu cassino da sorte espera por voce Blaze

  2. If some one desires expert view about blogging and site-building afterward i
    suggest him/her to pay a quick visit this blog, Keep up the pleasant work.

  3. 3 Common Causes For Why Your Online Shopping Sites In Uk For Electronics Isn’t Performing (And What You Can Do To Fix It) stainless steel hand blender

  4. The People Closest To Online Shopping Uk Amazon Share Some Big Secrets Poster Frame 15X33

  5. How To Outsmart Your Boss In Upvc Windows Repairs Repairing Upvc Windows

  6. Morgan表示:

    The Reasons Examples Of Online Products Is The Most Popular Topic In 2023 Sterilite With Plum Handles (Morgan)

  7. You’ll Be Unable To Guess Federal Employers’s Benefits federal employers

  8. PORN表示:

    Hello to every one, it’s truly a pleasant for me to visit this web site,
    it consists of priceless Information.

  9. Slot Wins表示:

    Guide To Slot Wins: The Intermediate Guide Towards Slot Wins Slot Wins

  10. Hey There. I found your weblog the usage of msn. That is a really well written article.
    I will make sure to bookmark it and return to read more of your helpful information. Thank you for the post.
    I’ll certainly comeback.

  11. I don’t know whether it’s just me or if everybody else
    experiencing problems with your site. It appears as though some of the written text
    within your posts are running off the screen. Can somebody
    else please comment and let me know if this is happening to them as well?
    This may be a problem with my web browser because I’ve had this happen before.
    Thanks

    Also visit my web blog :: toddler zip towel

  12. 12 Companies Are Leading The Way In Slot Variance Exciting Casino Slots

  13. WOW just what I was searching for. Came here by searching
    for tote bag hush puppies

  14. Storage Bed表示:

    If you desire to obtain a good deal from this paragraph then you have to
    apply such strategies to your won web site.

    Also visit my web-site: Storage Bed

  15. 5 Must-Know Slot Mechanics-Practices You Need To Know For 2023 evoplay slots reels (http://Www.Asystechnik.com)

  16. You’ll Never Guess This Shopping Online Uk To Ireland’s Tricks shopping Online uk to Ireland

  17. Chassidy表示:

    9 Lessons Your Parents Teach You About Best Online Shopping Sites London best online shopping sites london (Chassidy)

  18. Blanche表示:

    One Key Trick Everybody Should Know The One Slot Gaming
    Trick Every Person Should Know video slots (Blanche)

  19. This Week’s Top Stories About Unique Slots Unique Slots High-Quality
    Slots; https://Www.Cheaperseeker.Com/,

  20. See What Demo Slot Zeus Rupiah Tricks The Celebs Are Making Use
    Of Demo Slot Zeus Rupiah

  21. Lynda表示:

    A Comprehensive Guide To Mercedes Key Programmer.
    Ultimate Guide To Mercedes Key Programmer mercedes
    a class key (Lynda)

  22. slot payouts表示:

    5 Killer Quora Answers On Best Slot Payouts slot payouts

  23. Is Does Amazon Ship To Uk The Best Thing There Ever Was? Cooking Knife Set For Beginners

  24. Jacklyn表示:

    The Reason Slot Developers Is Fast Becoming The Most Popular
    Trend In 2023 evoplay slots reels (Jacklyn)

  25. JeffreyGar表示:

    שלהם. כאן הגבר יכול ליהנות מזמן איכות שבו הוא נמצא במרכז העניינים. זהו המקום בו אתה יכול לשכוח מהעבודה ומכל הצרות של החיים, מוכנות להגשים את משאלותיכם! אל תדאגו יותר מדי כאשר אתם מסתכלים על אישה. הסתכלו על הרגע הזה, מצד שני – זה לא בגידה, אלא ניסיון בחורות סקס

  26. Very rapidly this web page will be famous amid
    all blogging and site-building visitors, due to it’s pleasant posts

  27. 10 Things You’ll Need To Learn About Popular
    Slots Scatter Slots (http://Bbs.Qupu123.Com)

  28. The Time Has Come To Expand Your Princess Demo Options gates of princess demo slot

發佈留言

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