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,727 Responses

  1. See What Locksmith Near Me For Car Tricks The
    Celebs Are Making Use Of locksmith near me for Car

  2. Hello! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in trading links or maybe guest authoring a blog post or
    vice-versa? My site covers a lot of the same subjects as yours and I feel we could greatly benefit from each other.
    If you’re interested feel free to shoot me an e-mail.
    I look forward to hearing from you! Terrific blog by the
    way!

  3. bokep jepang表示:

    Hello my family member! I wish to say that this post is
    awesome, nice written and include approximately all vital infos.
    I’d like to look extra posts like this .

  4. Three Greatest Moments In Double Glazed Repairs Near Me History double glazed Units; https://timeoftheworld.date,

  5. Hello there, just became alert to your blog through Google, and found that it’s really informative.
    I’m gonna watch out for brussels. I will appreciate if you continue this
    in future. Numerous people will be benefited from your writing.
    Cheers!

  6. Replacement表示:

    The Reason Double Glazing Companies Near
    Me Is Everyone’s Obsession In 2023 Replacement

  7. Hello! Someone in my Myspace group shared this site with us so I
    came to take a look. I’m definitely enjoying the
    information. I’m book-marking and will be tweeting this to my followers!
    Superb blog and wonderful style and design.

  8. windows表示:

    A Peek Into The Secrets Of Window Repair Near windows

  9. 10 Myths Your Boss Is Spreading Concerning Volvo V70 Key
    volvo s40 key fob not working

  10. Your style is unique compared to other folks I’ve read stuff from.
    I appreciate you for posting when you’ve got the opportunity, Guess I will just book mark this page.

  11. 7 Small Changes That Will Make A Big Difference With Your Mesothelioma Mesothelioma Attorney

  12. You’ll Never Be Able To Figure Out This Trusted Online Shopping Sites For
    Clothes’s Benefits Trusted Online shopping sites for clothes

  13. Volvo key fob表示:

    16 Must-Follow Pages On Facebook For Volvo C30 Key-Related Businesses
    Volvo key fob

  14. Asad Mushtaq表示:

    Hi, my name is Asad, I have noticed a few issues with your website, and I’d love to help you fix them as I am an SEO Specialist and help small business to get more business using thier website? Should I send you issues list? if yes, reply with yes

  15. Hi there! This post couldn’t be written any better!
    Looking through this article reminds me of my previous roommate!
    He always kept talking about this. I most certainly will forward this
    article to him. Pretty sure he’ll have a very good read. Many thanks for sharing!

  16. Nice post. I was checking constantly this blog and I’m impressed!

    Extremely helpful info particularly the last part :
    ) I care for such information much. I was looking for
    this certain info for a very long time. Thank you and best of luck.

  17. Howdy this is kinda of off topic but I was wondering if blogs use WYSIWYG
    editors or if you have to manually code with HTML.

    I’m starting a blog soon but have no coding knowledge so I wanted to get guidance
    from someone with experience. Any help would be enormously appreciated!

  18. The Companies That Are The Least Well-Known To Keep An Eye On In The L
    Shaped Couches For Sale Industry double chaise Couch (https://www.cheaperseeker.com/)

  19. Great web site you’ve got here.. It’s hard to find good quality
    writing like yours these days. I honestly appreciate individuals like you!
    Take care!!

  20. The Biggest Sources Of Inspiration Of Sofa Sale Clearance sectional sofas for sale

  21. What To Say About Double Bed Top Bunk To Your Mom double size bunk beds (https://securityholes.science/)

  22. I know this if off topic but I’m looking into starting my own weblog 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 web savvy so I’m not 100% positive. Any recommendations or advice
    would be greatly appreciated. Cheers

  23. 10 Facts About Mini Cooper Key Fob Replacement That Insists On Putting
    You In A Good Mood mini cooper keys

  24. Car Lovksmith表示:

    See What Car Lovksmith Tricks The Celebs Are Making
    Use Of Car Lovksmith

  25. Today, I went to the beach front with my kids. I found a sea shell and gave
    it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and
    screamed. There was a hermit crab inside and it pinched her ear.

    She never wants to go back! LoL I know this is totally off topic but I had
    to tell someone!

  26. Five Killer Quora Answers To Double Glazed Near Me
    double glazed near Me

  27. 5 Killer Quora Answers On Motorcycle Accident Law Motorcycle Accident

  28. Alfredo表示:

    Folding Mobility Scooters Near Me Tips From The Most Effective In The Industry buy mobility scooter
    near me (Alfredo)

  29. The 9 Things Your Parents Teach You About Gotogel
    Link Alternatif Gotogel Link Alternatif, http://Ads.Westfunk.De/,

發佈留言

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