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,157,956 Responses

  1. Nexia789表示:

    My partner and I stumbled over here from a different website and thought I may as
    well check things out. I like what I see so now i’m following you.
    Look forward to looking over your web page again.

  2. link bokep表示:

    Hello there! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really appreciate your content.

    Please let me know. Many thanks

  3. Hmm it appears like your blog ate my first comment (it was extremely long) so I guess I’ll
    just sum it up what I had written and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog writer but I’m still new to everything.
    Do you have any tips and hints for newbie blog writers?
    I’d genuinely appreciate it.

  4. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://befitjusteat.pl/kawgkh of type and scrambled it to make a type specimen book.

  5. fair slots表示:

    15 Incredible Stats About Slot Sound Effects fair slots

  6. Aurelio表示:

    Kudos, Ample posts.

  7. Car Accidents表示:

    The Top Car Accident Lawyers Gurus Are Doing Three Things Car Accidents

  8. TAMPUNG99表示:

    Undeniably believe that which you said. Your favorite reason appeared to be
    on the internet the simplest thing to be aware of. I say to
    you, I definitely get annoyed while people think about worries that they plainly do not know about.
    You managed to hit the nail upon the top and also defined out
    the whole thing without having side-effects , people can take a signal.
    Will likely be back to get more. Thanks

  9. Personal Injury Lawyers Tools To Streamline Your Everyday Lifethe Only Personal Injury Lawyers
    Trick That Should Be Used By Everyone Know personal injury lawyers

  10. 15 Things You’re Not Sure Of About Wordai Versus Article Rewriter What is Word ai

  11. 5 Laws That Will Help The Motor Vehicle Claim Industry motor vehicle accident law firms

  12. bokep viral表示:

    I’m no longer sure the place you’re getting your information, however good topic.
    I needs to spend a while finding out more or figuring out more.
    Thank you for wonderful info I was on the lookout for this info
    for my mission.

  13. Ganhe muito no melhor cassino Blaze

  14. 20 Tips To Help You Be More Efficient With Birth Injury Compensation Birth Injury Lawsuits

  15. 10 Wrong Answers To Common Repairs To Upvc Windows Questions Do You Know Which
    Ones? upvc Window repair

  16. Your Family Will Thank You For Having This SEO Consulting Company Top content marketing Companies

  17. Hey there! I know this is kind of off-topic but I needed to ask.
    Does managing a well-established website like yours require a massive
    amount work? I’m brand new to writing a blog however I do
    write in my diary on a daily basis. I’d like to start a blog so
    I can share my personal experience and views online.
    Please let me know if you have any kind of recommendations or tips for brand new aspiring bloggers.
    Thankyou!

  18. What Is Slot Gaming And Why Is Everyone Dissing It? casino slots guide

  19. Seven Explanations On Why Birth Defect Settlement Is Important birth defects (https://Dasibogi.live/)

  20. Amazing things here. I’m very happy to look your article.
    Thanks so much and I’m taking a look forward to touch
    you. Will you kindly drop me a mail?

  21. Ashton表示:

    My spouse and I absolutely love your blog and find nearly all of your post’s
    to be what precisely I’m looking for. Would you offer guest
    writers to write content for you personally?
    I wouldn’t mind writing a post or elaborating
    on a number of the subjects you write about here.
    Again, awesome blog!

  22. Utilisez des stratégies de segmentation et de targeting créatives pour maximiser vos performances.

  23. RicardoNak表示:

    Thibaut Nicolas Marc Courtois https://thhibaut-courtois.prostoprosport-fr.com Footballeur belge, gardien de but du club espagnol du Real Madrid . Lors de la saison 2010/11, il a ete reconnu comme le meilleur gardien de la Pro League belge, ainsi que comme joueur de l’annee pour Genk. Triple vainqueur du Trophee Ricardo Zamora

  24. The Most Negative Advice We’ve Ever Received On Slot Strategies
    slot payouts (vdruzhkovke.net.Byjeanne.Com)

  25. One Auto Accident Attorney Success Story You’ll Never Be Able To
    Auto Accident Lawsuits

  1. 2024-07-01

    how long does valtrex stay in your system

    how long does valtrex stay in your system

發佈留言

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