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,157,241 Responses

  1. Good day! This is my first visit to your blog! We are a team
    of volunteers and starting a new project in a community in the same niche.
    Your blog provided us valuable information to work on. You have done a wonderful job!

  2. оборудование конференц залов оборудование конференц залов .

  3. конференц-залы под ключ конференц-залы под ключ .

  4. 10 Facts About Slot Updates That Will Instantly Get You Into A Great Mood entertaining slots

  5. Online Shopping Websites Clothes Tools To Ease Your Daily Life Online
    Shopping Websites Clothes Trick That Every Person Should Learn online shopping websites clothes

  6. The Myths And Facts Behind Loft Beds With Desk Low loft bed frame

  7. Window Repair Near Me Techniques To Simplify Your Daily Lifethe One Window Repair Near Me Trick That Every Person Should Be Able To Window Repair Near Me

  8. See What Adhd Assessment Tricks The Celebs Are Utilizing adhd Assessment women

  9. 20 Irrefutable Myths About Mesothelioma Litigation: Busted Mesothelioma lawyer

  10. Who Is Responsible For An Cerebral Palsy Litigation Budget?

    12 Best Ways To Spend Your Money Cerebral Palsy Lawsuits, Losguerrerosdeoracion.Com,

  11. Slot wins表示:

    So , You’ve Purchased Slot Sound Effects … Now What?
    Slot wins

  12. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://propcraft.pl/gaavnjn of type and scrambled it to make a type specimen book.

  13. r.asajikan.jp表示:

    The Comprehensive Guide To Machu Picchu Coffee Beans 1kg 1kg arabica coffee beans (r.asajikan.jp)

  14. ค้นหา โลกแห่งการ ‘SlotXO’

    ‘SlotXO’ เป็นการทดลอง โลกออนไลน์ที่เปิดกว้างให้กับผู้เล่นจำนวนมาก ด้วยความสะดวกและความปลอดภัยที่เหนือระดับ ผู้เล่นสามารถดำเนินการ เกมสล็อตออนไลน์ได้ทุกที่ทุกเวลา โดยเพียงแค่มีอุปกรณ์ที่เชื่อมต่ออินเทอร์เน็ต
    ไม่ว่าจะเป็นสมาร์ทโฟน แท็บเล็ต หรือคอมพิวเตอร์ก็ตาม

    ‘SlotXO’ นำเสนอ เกมสล็อตที่หลากหลายและน่าตื่นเต้น ด้วยกราฟิกและเอฟเฟกต์เสียงที่สมจริง ผู้เล่นจะได้เพลิดเพลิน กับประสบการณ์การเล่นเกมที่เหมือนจริงเสมือนอยู่ในบ่อนคาสิโนจริง ๆ นอกจากนี้ยังมีโบนัสและรางวัลมากมายให้ผู้เล่นได้ใช้ประโยชน์ ซึ่งจะทำให้การเล่นเกิดความตื่นเต้นและสนุกสนานยิ่งขึ้น

    ด้วยการคุ้มครอง ความปลอดภัยที่เข้มงวด ‘SlotXO’ ยังมีระบบการทำการตรวจสอบ ที่ปลอดภัยและเชื่อถือได้ ผู้เล่นสามารถเบิก เงินได้อย่างสะดวก รวดเร็ว และปลอดภัย นับเป็นการเปิดโอกาส ให้ผู้เล่นได้เพลินใจ กับประสบการณ์การเล่นเกมสล็อตออนไลน์ที่ดีที่สุด

    ในภาพรวม ‘SlotXO’ เป็นหน้าต่าง
    ที่น่าสนใจสำหรับผู้ที่หลงใหล การเล่นเกมสล็อตออนไลน์ ด้วยความครบครัน
    ของเกม ความปลอดภัยสูง และชิง
    ในการรับรางวัล ซึ่งจะทำให้ผู้เล่นได้รับความตื่นเต้น ในการเล่นเกมอย่างแน่นอน

    my blog post ข้อกำหนดในการเดิมพัน – hikvisiondb.webcam,

  15. slot gacor表示:

    Why people stiⅼl սse to reɑd news paperrs ԝhen inn this technplogical ԝorld the wһole tһing іs available оn web?

    Feel frewe tⲟ surf to my webpage … slot gacor

  16. 20 Things You Need To Be Educated About Upvc Windows Near
    Me Upvc Window Repair

  17. Guide To Online Shopping Uk Cheap: The Intermediate Guide
    Towards Online Shopping Uk Cheap Online Shopping Uk Cheap (http://Www.Russiacompany.Ru)

  18. W3schools表示:

    This website was… how do I say it? Relevant!! Finally I have found something that helped me.
    Thank you!

  19. sofas sale表示:

    5 Killer Quora Answers To Sofas Sale sofas sale

  20. 5 Killer Quora Answers On Best 18 Wheeler Accident Attorneys 18 Wheeler Accident Attorney (http://Www.Google.Com)

  21. What’s The Reason? Erb’s Palsy Case Is Everywhere
    This Year erb’s Palsy lawyers

  22. Auto Accident Lawyers Tools To Ease Your Everyday Lifethe
    Only Auto Accident Lawyers Technique Every Person Needs To Know auto accident lawyers

  23. Arielle表示:

    You’ll Never Be Able To Figure Out This Shopping Online Uk To Ireland’s Secrets shopping
    online uk to ireland (Arielle)

發佈留言

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