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,234,638 Responses

  1. Hello there, just became aware of your blog through Google,
    and found that it is really informative. I’m going to watch
    out for brussels. I’ll appreciate if you continue this in future.
    Numerous people will be benefited from your writing.

    Cheers!

  2. I know this site presents quality dependent content and
    additional data, is there any other site which presents these kinds of things in quality?

  3. I could not refrain from commenting. Very well written!

  4. Hello, I check your blog on a regular basis. Your story-telling style is witty, keep it up!

  5. Thank you for the auspicious writeup. It in fact was
    a amusement account it. Look advanced to more added agreeable
    from you! By the way, how can we communicate?

  6. I am curious to find out what blog platform you’re working with?

    I’m experiencing some small security problems with my latest website and I’d like
    to find something more safeguarded. Do you have any recommendations?

  7. Wonderful work! That is the kind of information that are supposed to be shared across the internet.

    Disgrace on the seek engines for not positioning this submit higher!
    Come on over and seek advice from my website . Thank
    you =)

  8. Hurrah, that’s what I was exploring for, what a material!
    present here at this blog, thanks admin of this site.

  9. Keep on writing, great job!

  10. Excellent article. I’m going through a few of these issues as well..

  11. Great blog here! Also your website loads up very fast! What host are you using?

    Can I get your associate hyperlink for your host?
    I desire my website loaded up as quickly as yours lol

  12. Slot Bank Neo表示:

    I am truly grateful to the owner of this web page who has shared this fantastic piece of writing at
    at this time.

  13. I couldn’t refrain from commenting. Perfectly written!

  14. Greate pieces. Keep writing such kind of information on your blog.
    Im really impressed by it.
    Hi there, You have done a great job. I will definitely digg it
    and in my opinion recommend to my friends. I’m confident they
    will be benefited from this web site.

  15. Hi there! I know this is kind of off topic but I was
    wondering which blog platform are you using for this website?

    I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform.

    I would be fantastic if you could point me in the direction of a good platform.

  16. nordic walkng表示:

    Allerdings werden Nordic Walking Stöcke in Längenabständen von 5 cm (selten auch
    2,5cm) angeboten, so, dass im gegebenen Fall gerundet werden muss.
    Dabei tunlich gerade bei Einsteigern eher abzurunden und einen kürzeren Stock zu wählen. Als erfahrener Trainer kann
    man gerade durch (sinnvolle!) Variation der Stocklänge unterschiedliche Trainingsreize setzen. Fortgeschrittene dynamische Nordic Walker können ruhig auch mit einem größeren Stock arbeiten. Für
    Anfänger ist der Winkel sogar noch etwas größer zu wählen, Wohl Technik einfacher zu erlernen. Als zweite
    Möglichkeit (wenn man nicht immer rechnen möchte) dienlich die praktische Lösung: Stock und Unterarm sollten bei fest
    umfassten Griff und gerade aufgestelltem Stock einen leicht größeren Winkel als 90° ergeben. Stocklängen trotz gleicher Körpergröße optimal versorgt.
    Bei dieser Art der individuellen Stocklängenzuweisung
    werden auch verschiedene Menschentypen (z.B.
    4. Wie viel sollten Nordic Walking Stöcke kosten? Der Preis variiert
    abhängig deren Qualität des Schafts, vom Material beziehungsweise dem Carbonanteil
    als auch vom Schlaufensystem. Die Preisspanne für einen Qualitätsstock bewegt sich zwischen 40€ und 100€.

    Gelegenheitswalker bekommen zwischen 40-50€ das beste Preis-Leistungs-Verhältnis, Walker die ihre Stöcke öfter als 3mal diese Woche im Einsatz haben, sollten entsprechend nicht auf den Preis gucken.

  17. Hey there! I know this is somewhat off topic
    but I was wondering which blog platform are you using for this
    website? I’m getting sick and tired of WordPress because I’ve
    had issues with hackers and I’m looking at options for another platform.
    I would be awesome if you could point me in the direction of a good
    platform.

  18. Your style is very unique compared to other folks I have read stuff from.

    Thank you for posting when you have the opportunity, Guess
    I will just book mark this page.

  19. This is a very good tip especially to those fresh
    to the blogosphere. Simple but very accurate information… Thank
    you for sharing this one. A must read article!

  20. Its like you read my mind! You appear to know so much
    about this, like you wrote the book in it or something.
    I think that you can do with a few pics to drive the message home
    a little bit, but other than that, this is wonderful blog.
    A great read. I will definitely be back.

  21. Excellent blog post. I definitely love this website.
    Stick with it!

  22. I am extremely impressed with your writing talents
    and also with the layout in your weblog. Is this a paid subject or did you customize it yourself?

    Either way keep up the excellent high quality writing, it’s rare to look a nice blog
    like this one nowadays..

  23. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.

    You definitely know what youre talking about, why waste your intelligence
    on just posting videos to your weblog when you could be giving us something informative to read?

  24. Greetings! Very helpful advice within this post!
    It is the little changes that produce the largest changes.

    Thanks for sharing!

  25. As a Newbie, I am constantly exploring online for articles that can help me.

    Thank you

  26. Can I just say what a relief to discover someone who truly understands what they’re discussing on the web.
    You actually know how to bring an issue to light and make
    it important. More people really need to look at this and understand this side of
    your story. I can’t believe you’re not more popular since you surely have the
    gift.

  27. I got this web site from my buddy who informed me concerning this web
    site and now this time I am visiting this website and reading very informative posts at this time.

  28. I’m gone to inform my little brother, that he should also visit this webpage on regular basis to get updated from
    hottest reports.

  29. This is a topic that is near to my heart… Best wishes!
    Where are your contact details though?

  30. I visited many blogs but the audio feature for audio songs current at
    this website is actually wonderful.

發佈留言

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