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,371,323 Responses

  1. I pay a visit daily a few blogs and information sites
    to read content, however this weblog offers feature
    based writing.

  2. KennethPah表示:

    clomid tablets how to get clomid price cost of clomid now

  3. Nikefut表示:

    deep web sites darkmarket darknet websites

  4. Hi there everyone, it’s my first pay a quick visit at this web site, and paragraph is really fruitful in support of me,
    keep up posting these content.

  5. If some one needs expert view on the topic of blogging then i propose him/her to pay a quick visit this
    web site, Keep up the pleasant work.

  6. Jimmy Santos表示:

    This blog has some excellent advice for optimizing landing pages! More insights can be found at rockland seo

  7. Buzzwords, De-Buzzed: 10 Other Methods To Say Buy
    Real Driving License Experiences Deutschen Registrierten FüHrerschein Kaufen

  8. What are some top tips from professionals like those at #yourkeyword# for DIY decorations? christmas light installation near me

  9. Curtis表示:

    The 10 Most Scariest Things About Crypto Local Casino crypto local casino
    (Curtis)

  10. Good day! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m absolutely enjoying your blog and look forward to new posts.

  11. phising表示:

    I love what you guys are usually up too. This kind of clever work and
    exposure! Keep up the awesome works guys I’ve incorporated you guys to my own blogroll.

  12. ThelmaFange表示:

    On our website you will find unique collection rare coins from the whole planet. We offer both modern and historical specimens that will become real finds for collectors.
    For more information, follow the link [url=https://numizmatexpert.ru/]монета ежик в тумане 2024 [/url]
    Our team of experts carefully selects each coin, ensuring high quality.

  13. My spouse and I stumbled over here different web page
    and thought I might as well check things out.

    I like what I see so now i am following you.
    Look forward to looking into your web page yet again.

  14. KennethPah表示:

    amoxicillin without rx 875 mg amoxicillin cost amoxicillin medicine

  15. It’s difficult to find knowledgeable people about this topic, but you sound like
    you know what you’re talking about! Thanks

  16. Walter May表示:

    I learned so much about how to choose the right #moving company# through online Cleveland apartment movers

  17. I like what you guys are usually up too. This kind of clever work and
    coverage! Keep up the very good works guys I’ve added
    you guys to our blogroll.

  18. Walter表示:

    The Reasons Why Adding A Pragmatic To Your Life Will Make All The A Difference 프라그마틱 무료 슬롯버프 – Walter

  19. hello there and thank you for your info – I’ve definitely
    picked up something new from right here. I did however expertise a few technical points using this site, since I experienced to reload the
    website a lot of times previous to I could get
    it to load properly. I had been wondering if your hosting is
    OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and
    could damage your high-quality score if advertising and marketing with Adwords.
    Anyway I’m adding this RSS to my email and can look out for much more of your respective
    intriguing content. Ensure that you update this again very soon.

  20. DanielAbice表示:

    http://cipharmdelivery.com/# cipro ciprofloxacin

  21. KennethPah表示:

    amoxicillin generic cost of amoxicillin prescription medicine amoxicillin 500

  22. DanielAbice表示:

    https://clomidonpharm.com/# where can i get clomid prices

  23. EdgarWat表示:

    Интересные статьи о займах – https://dubna.ru/article/2022/04/qiwi-koshelek-teper-eshchyo-i-bystrye-zaymy-na-kartu

  24. I’m impressed, I have to admit. Rarely do I come across a blog that’s both educative and
    amusing, and without a doubt, you’ve hit the nail on the head.
    The problem is an issue that not enough men and women are speaking intelligently about.
    I am very happy I found this during my hunt for something relating to this.

  25. Alex Hammond表示:

    Here’s hoping everyone gets their chance on the water soon; we need more surfers in our world according to # # anyKeword fulcrum surf competition

發佈留言

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