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,431,007 Responses

  1. With the increasing popularity of Cash App in today’s digital
    world, having a verified account can offer a range of
    advantages. Buying a verified Cash App account ensures instant access to
    various features, increased security measures,
    and higher transaction limits. Skip the hassle of waiting for verification and enjoy seamless money transfers,
    user-friendly interface, and reliable customer support.
    Don’t miss out on the conveniences offered by a verified Cash App account – get one today!

  2. Remarkable! Its genuinely amazing piece ߋf writing, I have gоt much cclear idea οn thhe topic оf
    from this post.

    Hеre is mmy web рage situs penambah view story instagram

  3. Really tons of amazing material.

  4. 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?

    Visit my page; craiglistforsex

  5. Wow, this post is good, my sister is analyzing these kinds of things, thus I am
    going to inform her.

  6. You suggested this terrifically!

  7. While borrower continues reducing his obligation through periodical repayments, credit history automatically improves.
    In with the current economic day’s consumer oriented earth, firms are likely to with will be as easy as expressing God many thanks.
    What better way to reminisce about your childhood than by watching classic Christmas movies.

  8. togel online表示:

    Ahaa, its nice conversation concerning this piece of writing at this place at this web site,
    I have read all that, so at this time me also commenting here.

  9. RichardLak表示:

    sildenafil pills online generic sildenafil from canada sildenafil 100 capsules

  10. Heya i’m for the first time here. I found this board and
    I find It truly useful & it helped me out much. I hope to give something back and help others like you aided me.

  11. Buying a verified Cash App account has numerous advantages.
    With a verified account, you can send and receive unlimited money, access various financial features,
    and enjoy enhanced security measures. It eliminates transaction limits and provides
    peace of mind while conducting online transactions.

    So, if you’re looking for a hassle-free experience and greater financial freedom, consider purchasing
    a verified Cash App account today!

  12. 13h File dimension in bytes, lowest byte first.

    The first way is to easily use the information which it incorporates such as the filename and size.
    The internal data stored here is comparable however not an identical to that saved by MSX-DOS 1 and totally completely different from CP/M.
    A basic description of each of the fields is included
    right here. It’s not anticipated that specially written MSX-DOS 2 transient programs
    or MSX-DOS 1 or CP/M packages which are modified for MSX-DOS 2 will use the CP/M-compatible FCB capabilities, however the format
    of the FCBs used for these functions is given right here for reference.
    It’s not anticipated nonetheless that new transient applications will use
    this perform call or the APPEND atmosphere string. MSX-DOS maintains a
    list of “environment strings” in it’s knowledge section.
    Representing and matching the face: That is the last step of recognition in which system matching the changing information with explicit storage after which
    permit that particular person for entry. This stage helps in improving the face detection functionality of the
    system. This results in an affordable efficiency overhead
    however informs the fuzzer about the rise in code
    protection throughout fuzzing, which makes gray-box fuzzers extraordinarily efficient vulnerability detection instruments.

  13. click4R.com表示:

    %%

    Here is my web site – volkswagen keys (click4R.com)

  14. If some one needs to be updated with latest technologies afterward he
    must be visit this web site and be up to date all the time.

  15. cosdoll表示:

    I’m amazed, I must say. Rarely do I come across a blog that’s equally
    educative and amusing, and without a doubt,
    you’ve hit the nail on the head. The issue is something which too few men and women are speaking
    intelligently about. I’m very happy I found this in my hunt for something regarding this.

  16. If you’re looking for a hassle-free way to buy things online or send/receive money, a verified Cash App
    account is the way to go. With its reliable security features
    and easy-to-use interface, you can confidently make transactions without worrying about scams
    or fraud. Don’t waste any more time, get your verified Cash App account today and enjoy a seamless
    digital payment experience.

  17. Your style is unique in comparison to other people I have read stuff from.
    Thank you for posting when you have the opportunity, Guess I’ll just bookmark this page.

  18. If some one desires to be updated with latest technologies then he must be pay a visit this web site and be up to date all the time.

  19. furniture表示:

    Ι got what you mean, thank yoᥙ for putting up. Woh I am pleased to find tһiѕ
    website through google.

    Here is my pаge: furniture

  20. Hi there I am so excited I found your blog, I really found you
    by mistake, while I was searching on Digg for something
    else, Nonetheless I am here now and would just like to say kudos for a remarkable post and a all round thrilling blog (I also love
    the theme/design), I don’t have time to read it all at the moment but I have saved
    it and also added in your RSS feeds, so when I have time I will be back to
    read more, Please do keep up the excellent job.

  21. %%

    Check out my website; renault clio car key replacement (Suji.hijack7.Co.kr)

  22. I just like the helpful info you provide in your articles.
    I’ll bookmark your weblog and test again here regularly.
    I’m rather sure I will be informed lots of new stuff right here!

    Best of luck for the following!

  23. Great information. Lucky me I recently found your blog by accident (stumbleupon).

    I have book-marked it for later!

  24. Drtop.kr表示:

    %%

    Look into my blog post … mesothelioma litigation (Drtop.kr)

  25. Te.legra.ph表示:

    fireplace surrounds (Te.legra.ph)

  26. bosforo tour表示:

    Do you have a spam issue on this blog; I also am a blogger, and I was wondering your situation; many of us have created some
    nice practices and we are looking to swap
    solutions with other folks, why not shoot me an e-mail if interested.

  27. Buying a verified Cash App account has become
    a popular option for individuals who want to experience the convenience and benefits of this digital payment platform.

    With a verified account, users can enjoy higher transaction limits, enhanced security features, and a seamless experience.
    With the rise of online scams and fraudulent activities,
    purchasing a verified Cash App account ensures peace
    of mind and reliable service. Whether you need it for personal
    or business use, investing in a verified Cash App account is a
    smart decision that can save you time and provide a secure platform for your financial transactions.

發佈留言

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