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,229,948 Responses

  1. Hello, I read your blogs on a regular basis. Your writing style is awesome, keep up the good work!

  2. hhome表示:

    Hi there would you mind stating which blog platform you’re using?
    I’m going to start my own blog soon but I’m having
    a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most
    blogs and I’m looking for something completely unique. P.S Apologies for getting off-topic but I had
    to ask!

  3. hey there and thank you for your info – I’ve definitely picked up anything new from right here.
    I did however expertise a few technical issues 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 web host is OK? Not that I am
    complaining, but slow loading instances times will often affect your placement in google and can damage your
    quality score if ads and marketing with Adwords. Anyway I’m adding this RSS to my
    email and can look out for much more of your respective fascinating content.
    Ensure that you update this again soon.

  4. Easydrorbix表示:

    cialis discount pharmacy: wedgewood pharmacy gabapentin – online pharmacy pain relief

  5. 20 Fun Details About Audi Key audi car key Holder (g28carkeys69632.ssnblog.com)

  6. Easydrorbix表示:

    manor pharmacy store locator: the pharmacy – cephalexin pharmacy

  7. The 10 Most Terrifying Things About Titration Meaning ADHD titration meaning adhd (https://www.instapaper.com/)

  8. 15 Terms Everyone Who Works In Desk Treadmill Industry Should Know under desk Treadmill

  9. Why Nobody Cares About Bluetooth Fucking Machine Sex Machine Adult

  10. Easydrorbix表示:

    abortion pill online pharmacy: Prasugrel – pharmaceuticals online australia

  11. sitee表示:

    I am really glad to glance at this web site posts which carries plenty of valuable information, thanks for
    providing these kinds of information.

  12. home表示:

    Hi there, all is going nicely here and ofcourse every one is sharing information, that’s truly fine, keep up writing.

  13. Cazrjic表示:

    Привет!
    Мы предлагаем документы техникумов
    varanasitutor.com/questions/kupit-diplom-582279iol

  14. www表示:

    obviously like your web-site but you need to take a look at
    the spelling on several of your posts. Many of them are
    rife with spelling issues and I to find it very bothersome
    to tell the truth nevertheless I’ll certainly come back again.

  15. In Which Location To Research Treat Anxiety Online is anxiety Treatable – http://www.Annunciogratis.net,

  16. Easydrorbix表示:

    tamoxifen online pharmacy: viagra from pharmacy – legitimate online pharmacies

  17. This article is actually a good one it assists new internet users, who are wishing in favor of blogging.

  18. Terrific post but I was wondering if you could write a litte more on this subject?
    I’d be very thankful if you could elaborate a little bit further.
    Thank you!

  19. Easydrorbix表示:

    pharmacy rx 1: medicine online order – abilify india pharmacy

  20. The Guide To Car Key Cutting Machine In 2023 cheapest key cutting Near me

  21. Easydrorbix表示:

    australian pharmacy online: sam’s club pharmacy propecia – fred’s dollar store pharmacy

  22. Karry表示:

    Thanks for a marvelous posting! I actually enjoyed reading it, you will be a
    great author. I will be sure to bookmark your blog and will
    eventually come back very soon. I want to encourage that you continue your great job, have a nice holiday weekend!

  23. Easydrorbix表示:

    valacyclovir indian pharmacy: xeloda specialty pharmacy – best indian pharmacy online

  24. Hola! I’ve been reading your web site for a while now and finally got the
    bravery to go ahead and give you a shout out from Austin Tx!
    Just wanted to say keep up the excellent job!

  25. porntube.red表示:

    Hello, its nice paragraph regarding media print,
    we all be familiar with media is a wonderful source of facts.

    Here is my web-site 成人卡通 (porntube.red)

  26. I think everything said was actually very logical.
    However, what about this? suppose you composed a catchier
    title? I mean, I don’t wish to tell you how to run your website, but what if you added
    a post title to possibly get a person’s attention? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室
    is a little plain. You ought to peek at Yahoo’s home page and watch how they write post headlines to get viewers to open the links.
    You might try adding a video or a related picture or two to get people excited about what you’ve got to say.
    In my opinion, it might bring your posts a little bit more interesting.

  27. Drstobeese表示:

    finasteride online pharmacy india: online pharmacies uk – pharmacy store design

  28. Drstobeese表示:

    reputable indian online pharmacy: pharmacy dispensing clozapine – mirtazapine online pharmacy

發佈留言

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