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,364,546 Responses

  1. Frankevild表示:

    https://ozempic.art/# ozempic cost

  2. Great weblog right here! Additionally your site rather a lot up fast!

    What host are you using? Can I am getting your affiliate link in your host?
    I want my site loaded up as fast as yours lol

  3. LeroyAcuff表示:

    Профессиональный сервисный центр ремонт андроид телефонов ближайший ремонт сотовых

  4. Ernest Booze表示:

    Five Car Lock Smith Lessons From The Pros car keys locksmith near me

  5. Watch This: How Integrated Fridge Freezer Is Gaining Ground And How To Respond tall integrated fridge freezer 50 50

  6. Thanks for your marvelous posting! I genuinely enjoyed reading it, you will be a great author.I
    will ensure that I bookmark your blog and may come back in the future.
    I want to encourage that you continue your great work, have a nice weekend!

  7. What’s The Current Job Market For Modular Sleeper Sofa Professionals Like?modular sleeper Sofa

  8. Guide To Best Retro Fridge Freezer: The Intermediate Guide Towards Best Retro Fridge Freezer best retro fridge freezer

  9. нарколог на дом вывод из запоя на дому нарколог на дом вывод из запоя на дому .

  10. 12 Stats About Situs 4d To Make You Seek Out Other People toto online terbaik (Marlon)

  11. 10 Things That Your Family Taught You About Upvc Windows And Doors Upvc Window

  12. iconwin表示:

    It’s impressive that you are getting ideas from this paragraph as well as from our argument made at this time.

  13. Jeremypag表示:

    cheapest rybelsus pills rybelsus pill rybelsus coupon

  14. 14 Questions You Might Be Refused To Ask Peugeot Boxer Key keys

  15. Andres表示:

    What’s Happening i am new to this, I stumbled upon this I’ve
    found It absolutely helpful and it has helped me out loads.
    I’m hoping to give a contribution & aid other users like its aided
    me. Good job.

  16. The provided substance of this blog piece is highly intriguing .

    I enjoyed the way you scrutinized the various issues so extensively
    and distinctly . You facilitated me procure fresh perspectives
    that I never considered before. I appreciate for sharing your
    mastery and competence – it has enabled me to improve myself further
    .
    I particularly liked the innovative viewpoints you introduced , which enlarged my mindset
    and reasoning in significant directions . This blog is systematic
    and enthralling, which is essential for information of this level .

    I hope to review additional of your compositions in the days
    to come , as I’m confident it is sure to continue to be informative and
    assist me continue growing . I express my gratitude !

    Also visit my web blog; advantages of playing at mobile casinos

  17. See What Treat Generalized Anxiety Disorder Tricks The Celebs Are Using Treat Generalized Anxiety Disorder

  18. What’s The Current Job Market For Coffee Machines Capsules Professionals?coffee Machines capsules

  19. 11 “Faux Pas” That Are Actually Okay To Create With Your Pragmatic Free 프라그마틱 사이트

  20. Sweet blog! I found it while searching on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thank you

  21. Jeremypag表示:

    rybelsus pill rybelsus cost rybelsus price

  22. Leif Felder表示:

    See What Kids Beds Bunk Beds Tricks The Celebs Are Using kids beds bunk

  23. Darby Ricardo表示:

    Why Land Rover Replacement Keys Isn’t A Topic That People Are Interested In Land Rover Replacement Keys land Rover key cutting And programming

  24. BarryDof表示:

    https://ozempic.art/# ozempic coupon

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

  26. Great work! This is the type of information that are supposed to be shared across the web.
    Disgrace on Google for not positioning this publish upper!
    Come on over and consult with my site . Thank you =)

  27. Jeremypag表示:

    buy rybelsus online rybelsus cost rybelsus pill

  28. KevinBothe表示:

    перейти на сайт Kraken19.at

  29. I do agree with all the concepts you have introduced
    to your post. They’re really convincing and will certainly work.
    Nonetheless, the posts are too brief for starters.
    May just you please extend them a little from subsequent
    time? Thanks for the post.

發佈留言

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