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,629,618 Responses

  1. I have learn several excellent stuff here. Definitely worth
    bookmarking for revisiting. I wonder how much
    attempt you set to make the sort of fantastic informative web site.

  2. Узнай все о признаки варикоцеле степени варикоцеле

  3. 1xbet表示:

    {

    Here is my web page … 1xbet

  4. Bradleygoath表示:

    can i purchase clomid rexpharm can i purchase cheap clomid without a prescription

  5. BrandenSlomy表示:

    buy dapoxetine online: Priligy tablets – dapoxetine price

  6. Danieljek表示:

    amoxicillin online pharmacy https://amoxilcompharm.com/# amoxil generic

  7. BrandenSlomy表示:

    priligy max pharm: buy priligy – dapoxetine price

  8. Jika Anda menginginkan cara yang lebih mudah dan nyaman untuk
    mengunduh video YouTube, Anda dapat berlangganan layanan YouTube Premium.

  9. Bradleygoath表示:

    generic prednisone cost raypharm can i buy prednisone online without prescription

  10. Danieljek表示:

    how to buy amoxycillin http://amoxilcompharm.com/# amoxicillin price canada

  11. Medicine information. Cautions.
    get generic bactrim tablets
    Everything news about meds. Get now.

  12. Dereketerm表示:

    buy amoxicillin online cheap: amoxicillin for sale online – amoxicillin tablets in india

  13. Danieljek表示:

    medicine amoxicillin 500 https://prednisoneraypharm.com/# buy prednisone without a prescription

  14. Bradleygoath表示:

    amoxicillin 250 mg capsule cheap amoxil buy cheap amoxicillin online

  15. The Next Big Event In The Car Crash Law Firm Industry Car Accident Lawyer Best

  16. I really like reading through an article that can make people
    think. Also, thanks for allowing for me to comment!

  17. you’re actually a excellent webmaster. The website loading
    speed is incredible. It kind of feels that you are doing any unique
    trick. Furthermore, The contents are masterwork. you have done
    a fantastic task on this subject!

  18. Loved reading about Rocket Queen. This was very helpful.

    Continue with the great writing.

  19. Thanks for sharing this! Lucky Jet is a game where timing is everything, and it’s always fun to
    see how other players approach it. I really
    enjoyed the perspective here.

  20. allegron表示:

    Thankfulness to my father who informed me about this website, this webpage is really awesome.

  21. Salutations, counterpart reader. I must compliment the author for their discerning and expertly-penned blog post.

    The material was both insightful and thought-provoking, leaving
    me with a heightened understanding of the matter at hand.

    I would want to extend an proposal to join the prestigious PUSSY888 community.

    This environment offers a sphere of fun and exhilaration, accommodating
    those who adore the more sophisticated things in experience.
    I suggest you to delve into the varied offerings and submerge yourself in the exciting explorations that draw you.

    Your presence would be particularly accepted, and I anticipate the
    privilege to engage with you more extensively within this
    exalted internet-based world

    Look into my page; online casino user experience (doodleordie.com)

  22. Good response in return of this difficulty with real arguments and explaining
    all about that.

發佈回覆給「Bradleygoath」的留言 取消回覆

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