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,466,936 Responses

  1. 부산表示:

    How Shell Out Less On Family Entertainment 부산

  2. Gabrielle表示:

    I was suggested this web site via my cousin. I am not sure whether this submit is
    written by way of him as no one else understand such certain approximately my problem.

    You’re amazing! Thanks!

  3. Wallacefex表示:

    slot oyunlar? puf noktalar?: slot oyunlar? puf noktalar? – slot oyunlar? puf noktalar?

  4. Leah表示:

    The Next Big Thing In American Style Fridge
    american style fridge freezer with ice maker, Leah,

  5. Lenora Garza表示:

    I liked this article. For additional info, visit pool service near me

  6. Niki表示:

    I believe everything wrote made a ton of sense. However, consider this,
    what if you added a little information? I mean, I don’t wish to tell you how
    to run your website, however suppose you added a title that grabbed people’s attention? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is
    a little vanilla. You might look at Yahoo’s front
    page and note how they create post titles to get viewers to click.
    You might try adding a video or a pic or two to grab readers interested about what you’ve written. Just my opinion,
    it would bring your blog a little bit more interesting.

  7. Nicely done! Find more at sod contractor

  8. Helena表示:

    Thanks for sharing such a good idea, paragraph is good, thats why i have read
    it fully

  9. Wallacefex表示:

    Casino Siteleri: Casino Siteleri – guvenilir casino siteleri

  10. A How-To Guide For Asbestos Cancer Law Lawyer Mesothelioma Settlement From Beginning
    To End Mesothelioma Lawyers

  11. sex表示:

    My coder is trying to persuade me to move to .net from
    PHP. I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type
    on a number of websites for about a year and am anxious about switching to another platform.
    I have heard fantastic things about blogengine.net.
    Is there a way I can import all my wordpress content into it?
    Any help would be really appreciated!

  12. Genie表示:

    Genuinely no matter if someone doesn’t know afterward its up to other people that they will assist, so here it occurs.

  13. Anuraenut表示:

    https://mostbet-wgb5.top/ официальную информацию о букмекерской конторе Мостбет и ее особенностях на сайте!

  14. deutsche xxxx表示:

    I’m gone to saay too my little brother, that hee should also visitt
    this website on reggular basiss to get updated
    feom newesst new update.

  15. I think the admin of this web page is really working hard in support of his web site, because here every stuff is
    quality based information.

  16. 연산동 경쟁 치열하죠? 그래도 저희 연산동 노래방알바가 콜은 제일 많습니다.

  17. Drug prescribing information. Effects of Drug Abuse.
    buying cheap macrobid without prescription
    Some information about medicament. Get information now.

  18. A Productive Rant About Lock Repair repair window locks upvc

  19. Corey表示:

    Very nice post. I just stumbled upon your weblog and wanted to say
    that I have really enjoyed browsing your blog posts.

    After all I’ll be subscribing to your rss feed and
    I hope you write again soon!

  20. The water slide was a hit at our summer gathering inflatable water slides to rent near me

  21. A private Instagram viewer is a tool or serve designed how to view private instagram profile
    allow users to view private Instagram accounts without needing to follow the account or acquire praise from
    the account owner. Typically, these viewers claim to bypass
    privacy settings and have enough money access to posts, stories,
    and supplementary content that would instead be restricted to followers.
    while some people may use these tools out of curiosity or for social media analysis, its important to note that using such facilities raises gigantic ethical and
    authenticated concerns. Most of these spectators piece of
    legislation in a gray area, often violating Instagram’s
    terms of assistance and potentially putting users’ privacy and
    data at risk. In addition, many of these tools require users to solution surveys or allow personal information, which can lead to scams,
    phishing attempts, or malware infections. Instagram has strict policies
    next to unauthorized entrance to accounts and may allow legal take action next to both users and services functional in breaching privacy.
    then again of relying upon private Instagram viewers, it’s advisable to love users’ privacy settings and follow accounts in a genuine manner.

    If someone has made their account private, its generally a sign that they hope to limit access to their content, and these boundaries should be respected.

  22. Wallacefex表示:

    deneme bonusu veren yeni siteler 2025: Casino Siteleri – Canl? Casino Siteleri

  23. FrankEmine表示:

    guvenilir slot siteleri: slot casino siteleri – az parayla cok kazandiran slot oyunlar?

  24. Wallacefex表示:

    sweet bonanza: sweet bonanza oyna – sweet bonanza

  25. Danny表示:

    I like the helpful information you provide in your articles.
    I’ll bookmark your blog and check again here frequently.
    I’m quite certain I’ll learn many new stuff right here!

    Good luck for the next!

  26. Chris Adams表示:

    Thanks for the informative post. More at murfreesboro website design

  27. Cynthia表示:

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

  28. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# denemebonusuverensiteler25

  29. Greta表示:

    Link exchange is nothing else however it is simply placing the other
    person’s website link on your page at appropriate place and other person will
    also do similar in favor of you.

  30. 10 Undisputed Reasons People Hate Mesothelioma Asbestos Claim Mesothelioma Lawyers

發佈留言

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