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,524,988 Responses

  1. เนื้อหา ของบล็อกนี้ เร้าใจมากๆ ครับ
    ผมชอบวิธีการวิเคราะห์ ประเด็นต่างๆ อย่าง ครบถ้วนและ มีเหตุมีผลชัดเจน เป็นการช่วยให้ผู้อ่าน ทำความเข้าใจ ประเด็นได้ ในระดับลึกขึ้น มากขึ้น คุณเขียนได้อย่าง เป็นระเบียบ และ
    น่าเอาใจใส่ซึ่งเป็นสิ่งสำคัญสำหรับบทความระดับนี้
    นอกจากนั้น ผมยังชอบ
    ทัศนะ ใหม่ๆ ที่คุณได้นำเสนอ ซึ่งเป็นสิ่งที่ไม่เคย คิดมาก่อน มันช่วยขยาย มุมมอง และ ทักษะของผมไปในทิศทางที่ ขยายขอบเขตขึ้น ผมขอขอบคุณที่คุณได้
    สื่อสารความรู้และประสบการณ์
    ของคุณ มันช่วยให้ผมได้ ก้าวไปข้างหน้ามากขึ้นอย่างแน่นอน
    ผมหวังว่าจะได้ ติดตาม บทความอื่นๆ ของคุณในอนาคตเช่นกัน เพราะผมมั่นใจว่าจะมีประโยชน์ และเป็นการ เสริมสร้าง ความรู้ให้กับผมอย่างแน่นอน ขอบคุณมากครับ!

    my web-site: แบบฟอร์มการแทงหวย

  2. LeroyBak表示:

    best online pharmacies in mexico mexico pharmacy reputable mexican pharmacies online

  3. Private Assessments For ADHD: 11 Thing That You’re Failing
    To Do private adhd Assessments

  4. LeroyBak表示:

    mexican pharmaceuticals online Mexican pharmacy ship US buying prescription drugs in mexico online

  5. Daniele表示:

    Why Nobody Cares About Electric Wall Mounted Fires white wall mounted electric
    fireplace (Daniele)

  6. для работы зеркала не необходимо скачивать приложение, казино водка официальный сайт использовать дополнительные программы.

  7. Williamagric表示:

    cheap erectile dysfunction pills http://pharm24.pro/# online prescription for ed meds

  8. Great article! We are linking to this particularly great content on our site.
    Keep up the good writing.

  9. LeroyBak表示:

    buy prescription drugs online cheap drugs prescription drugs

  10. www.zgqsz.com表示:

    The 10 Scariest Things About Cot Sales Cot sales (http://www.zgqsz.com)

  11. Williamagric表示:

    errection problems http://pharm24.pro/# ed pills that work

  12. A片表示:

    What’s up it’s me, I am also visiting this web page on a
    regular basis, this web page is in fact good and the visitors are truly sharing good thoughts.

    Take a look at my web blog: A片

  13. LeroyBak表示:

    erection pills cheaper medications online ed pills

  14. AwbHesia表示:

    Does a health problem need to be treated with levitra generic to get low prices for this effective treatment

  15. You’ll Never Guess This 3 Wheel Double Buggy’s Tricks 3 wheel Stroller with seat

  16. Jorgeagopy表示:

    buying prescription drugs in mexico online: mexico pharmacy cheap – buying from online mexican pharmacy

  17. Diplomi_llOl表示:

    купить диплом охраны diplomdarom.ru .

  18. togel4d表示:

    Simply desire tօ sаy y᧐ur artichle іs ass surprising.
    The clarity іn ʏour put uр iѕ simply geeat аnd thɑt і cօuld think
    you are an expert on thіѕ subject. Finne together wіtһ yоur permission ɑllow me to
    seize youг RSS feed tо keep updated wіth comіng neaг neаr post.
    Τhank you ⲟne mіllion andd please carry on the enjoyable work.

    Take a ⅼook аt mү blog post – togel4d

  19. LeroyBak表示:

    mail order pharmacy india Indian pharmacy online Online medicine home delivery

  20. slot gacor表示:

    whoah this weblog is wonderful i really like reading your posts.
    Keep up the great work! You recognize, many people are hunting around for this information, you
    could aid them greatly.

  21. Aurelio表示:

    The Top Prams For Sale Tricks To Transform Your Life top prams – Aurelio

  22. Diplomi_muma表示:

    где можно купить диплом где можно купить диплом .

  23. I was curious if you ever considered changing the layout of your blog?

    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.

    Youve got an awful lot of text for only having
    1 or two images. Maybe you could space it out better?

    https://paitosgpwarna.net/

  24. My spouse and I stumbled over here from a different
    web address and thought I may as well check thins
    out. I like what I see so noow i am following you.
    Look forward to checking oout ylur web page repeatedly.

    Check out myy webpage – http://Rc-Sibir.Ru/

  25. Diplomi_qpOl表示:

    купить диплом московская область diplomdarom.ru .

發佈留言

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