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,249,177 Responses

  1. Heya are using WordPress for your blog platform?
    I’m new to the blog world but I’m trying to get started and
    set up my own. Do you require any coding knowledge to make your own blog?
    Any help would be really appreciated!

  2. Irene Lloyd表示:

    Robótki ręczne to nie tylko hobby robótki ręczne na szydełku

  3. I think that is one of the most important info for me.
    And i’m satisfied studying your article. But want to statement on some
    normal things, The site style is ideal, the articles is really excellent :
    D. Excellent activity, cheers

  4. Rickie Allain表示:

    many way to consider when buying penthouse

  5. Grеetings! I knoԝ this is sߋmewhat off tipic but I was ѡondewring iif you knew where I cоuld get a captcha
    plugin for my commkent form? I’m using the same blg platform
    aѕ yοurs and I’m havbing trouble fіndkng one?
    Thanks a lot!

    Also vist my homepage: iconwin link alternatif

  6. nodeposit表示:

    Every weekend i used to pay a quick visit this web site, because i want enjoyment, for the reason that this
    this website conations really nice funny data
    too.

  7. Предлагаем услуги профессиональных инженеров офицальной мастерской.
    Еслли вы искали официальный сервисный центр lg, можете посмотреть на сайте: сервисный центр lg в москве
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  8. Experience hassle-free software installation and unparalleled support with Software Sale Mart.

  9. DarrenUnjuh表示:

    http://lisinopril1st.com/# zestril 10mg price

  10. I absolutely love your website.. Pleasant colors & theme.
    Did you develop this site yourself? Please reply back as I’m looking to create my own personal website and would
    love to learn where you got this from or just what the theme is called.
    Many thanks!

    https://w3.syairsetan.live/

  11. DarrenUnjuh表示:

    https://lisinopril1st.com/# cheapest Lisinopril

  12. Hello there! 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.

  13. I used to be suggested this web site by my cousin. I’m now not certain whether or not this submit
    is written via him as no one else realize such special about my trouble.
    You’re wonderful! Thank you!

  14. and if personally already have a buy doxycycline online uk on operating system android, study address amazon appstore
    for android, because that the right tablets require the right apps!

  15. BernardMop表示:

    cheapest Lisinopril cheapest Lisinopril Lisinopril 1st

  16. DarrenUnjuh表示:

    http://lisinopril1st.com/# Lisinopril 1st

  17. BrandenSlomy表示:

    buy cheap amoxicillin online: buy amoxil online – medicine amoxicillin 500

  18. Daniel Ortiz表示:

    I’m planning to do it yourself my roofing system repair work, however I’m nervous about it! Any recommendations from knowledgeable roofing professionals would be appreciated roof repairs in tampa

  19. many have own test of design and furnishing

  20. BrandenSlomy表示:

    steroids prednisone for sale: buy prednisone – prednisone capsules

  21. DarrenUnjuh表示:

    http://lisinopril1st.com/# buy Lisinopril 1st

  22. This Article表示:

    What you wrote made a ton of sense. But, consider this, what if you
    added a little content? I am not saying your information is
    not good., however what if you added something
    that grabbed people’s attention? I mean JS 依照元素出現數量排序 – 馬老師
    雲端研究室 is kinda boring. You ought to peek at Yahoo’s home page and note how they create post headlines to get people interested.

    You might try adding a video or a pic or two to grab
    people interested about what you’ve got to say. In my opinion, it could make your blog a little bit more interesting.

  23. DarrenUnjuh表示:

    http://lisinopril1st.com/# Lisinopril 1st

  24. Viagra Pill表示:

    Pretty section of content. I just stumbled upon your web site
    and in accession capital to assert that I acquire actually enjoyed account your blog posts.
    Anyway I’ll be subscribing to your feeds and even I achievement you access consistently quickly.

  25. What’s up colleagues, pleasant paragraph and good
    urging commented at this place, I am genuinely enjoying by these.

發佈留言

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