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,368,935 Responses

  1. BrandenSlomy表示:

    priligy maxpharm: buy dapoxetine online – Priligy tablets

  2. Markdob表示:

    Всем привет! Может кто знает, где почитатьполезные блоги о недвижимости? Пока нашел – https://glavtorgmsk.ru

  3. BrandenSlomy表示:

    buy amoxicillin over the counter uk: amoxicillin 500mg for sale uk – amoxicillin 500mg capsules price

  4. StevenNub表示:

    where buy cheap clomid no prescription: rex pharm – buy generic clomid

  5. Markdob表示:

    Всем привет! Может кто знает, где почитатьразные блоги о недвижимости? Сейчас читаю – https://glavtorgmsk.ru

  6. cuaca778.com表示:

    You are so awesome! I do not suppose I’ve truly read through
    something like that before. So nice to find another person with a few original
    thoughts on this subject matter. Seriously.. many thanks
    for starting this up. This site is one thing that’s needed on the internet,
    someone with a bit of originality!

  7. Your mode of telling everything in this article is genuinely good,
    all can effortlessly be aware of it, Thanks a lot https://www.provenexpert.com/en-gb/pub028/

  8. RPRP AI表示:

    Thanks for all your efforts on this blog. My mom loves making time for
    research and it is simple to grasp why. All of us notice
    all about the compelling means you make effective guidelines via your blog
    and in addition recommend response from visitors on this
    area of interest so our own girl is discovering a lot of
    things. Enjoy the remaining portion of the new year.
    You’re performing a fabulous job.[X-N-E-W-L-I-N-S-P-I-N-X]I am really inspired with your
    writing talents and also with the layout on your weblog.

    Is this a paid theme or did you modify it yourself?
    Either way keep up the nice quality writing,
    it is rare to see a nice weblog like this one nowadays.

  9. slot hari ini表示:

    Hey there, I think your site might be having browser compatibility issues.
    When I look at your blog in Chrome, it looks fine but when opening in Internet Explorer, it has
    some overlapping. I just wanted to give you a quick heads up!

    Other then that, amazing blog!

  10. Fern表示:

    I was recommended this web site by my cousin. I am not sure whether this post is written by him as no one else know
    such detailed about my trouble. You are incredible! Thanks!

  11. Hello there, I found your site via Google while
    searching for a related subject, your site came up, it seems to be good.
    I have bookmarked it in my google bookmarks.

    Hi there, simply changed into aware of your blog thru Google,
    and found that it’s really informative. I am gonna be careful for brussels.
    I will be grateful should you continue this in future.
    A lot of other people will probably be benefited from your writing.
    Cheers!

  12. Because the admin of this web site is working, no hesitation very quickly it will
    be renowned, due to its feature contents.

  13. BrandenSlomy表示:

    where to get cheap clomid prices: generic clomid – buying cheap clomid

  14. dewascatter表示:

    Hey there would you mind sharing which blog platform you’re using?
    I’m looking to start my own blog soon but I’m having a
    tough time selecting 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 unique.
    P.S Sorry for being off-topic but I had to ask!

  15. I’m not sure where you are getting your information, but great topic.
    I needs to spend some time learning much more or understanding more.

    Thanks for fantastic info I was looking for this info for my mission.

  16. Somebody necessarily lend a hand to make significantly posts I would state.
    This is the very first time I frequented your web page and to this
    point? I amazed with the research you made to make this particular publish incredible.
    Great process!

發佈留言

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