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

  1. 10 Websites To Help You Become An Expert In Male Adult Toys adult Toys Shop

  2. Wow! In the end I got a website from where I be capable of in fact get useful facts concerning my
    study and knowledge.

  3. Eartha Stepp表示:

    It Is Also A Guide To ADHD Diagnosis Private In 2023 private adhd assessment somerset

  4. Every weekend i used to pay a visit this website, as i want enjoyment, as this this web page conations really nice funny material too.

  5. Five Single Mattresses Sale Lessons Learned From Professionals thick single Mattress

  6. Jeremypag表示:

    buy cheap ozempic buy cheap ozempic ozempic coupon

  7. You’ll Never Be Able To Figure Out This Situstoto Slot’s Tricks situstoto Slot

  8. Frankevild表示:

    http://rybelsus.shop/# buy semaglutide pills

  9. Frankevild表示:

    https://rybelsus.shop/# rybelsus pill

  10. I know this if off topic but I’m looking into starting my own blog
    and was curious what all is needed to get setup? I’m assuming having a blog
    like yours would cost a pretty penny? I’m not very internet smart so I’m not 100% positive.
    Any suggestions or advice would be greatly appreciated. Thank you

  11. Frankevild表示:

    https://ozempic.art/# ozempic generic

  12. Jeremypag表示:

    ozempic cost ozempic coupon buy cheap ozempic

  13. 11 Ways To Fully Defy Your Mental Health Assessment mental health act Assessment

  14. 15 Gifts For The Double Glazed Hinges Lover In Your Life pvc window hinges

  15. В магазине сейфов предлагают сейф купить сейф в наличии

  16. A Retrospective What People Said About Replacement Key For Audi 20 Years Ago Audi etron key (m.jingdexian.com)

  17. syair hk表示:

    That is very interesting, You are an excessively professional blogger.
    I have joined your rss feed and look ahead to looking for extra of your fantastic
    post. Additionally, I’ve shared your website in my social networks

  18. This Week’s Most Popular Stories Concerning Metal Anal Butt Plug anal tops

  19. Jess Ming表示:

    Why Do So Many People Want To Know About Best Medication For Anxiety Disorder?Best Anxiety Disorder Treatment

  20. BarryDof表示:

    https://ozempic.art/# buy cheap ozempic

  21. Floyd表示:

    What’s The Current Job Market For Wood Burning Fire Professionals?

    wood burning fire (Floyd)

  22. Frankevild表示:

    https://ozempic.art/# ozempic

  23. Maryjo Yoo表示:

    How Foldable Wheelchair Ramp Propelled To The Top Trend In Social Media Foldable Ramp for wheelchair

  24. 5 Killer Quora Answers On Sliding Sash Windows sash Window restoration Near me

  25. Jeremypag表示:

    buy semaglutide pills semaglutide online rybelsus price

  26. bclub.cm表示:

    Greetings! This is my first visit to your blog! We are a team
    of volunteers and starting a new initiative in a
    community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!

    Here is my blog post :: bclub.cm

  27. Annie Chiodo表示:

    What Do You Need To Know To Be Prepared To Portable Mobility Scooters For Sale heavy duty mobility scooters for sale near me

  28. Frankevild表示:

    http://ozempic.art/# buy cheap ozempic

  29. What Is Bandar Online Togel? How To Utilize It bandar togel terpercaya (weinreich-link-3.blogbright.Net)

  30. This web site certainly has all of the info I wanted about this subject and didn?t know who to ask.

    my website; https://thanhdatmedical.vn/may-xquang-nha-khoa/

發佈留言

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