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,153,785 Responses

  1. 15 Things You Don’t Know About Mesothelioma Lawsuit mesothelioma settlement

  2. The Underrated Companies To Follow In The Free Casino Slots
    Industry online casino slots

  3. 9 Lessons Your Parents Teach You About Slot Sites slot sites [http://ling.teasg.tw/]

  4. 14 Questions You Might Be Refused To Ask Slot Innovations Casino slot machines

  5. 20 Truths About Automated Backlinking Software: Busted best Backlink Generator software

  6. action game表示:

    Hi there, just wanted to tell you, I liked this article. It was practical.
    Keep on posting!

  7. Danieldob表示:

    Приветствую. Может кто знает, где почитатьразные статьи о недвижимости? Пока нашел https://universal37.ru

  8. jisuzm.tv表示:

    ตั๋ว ประเภท ยี่กีเป็น ฟอร์ม การ เล่นเกม หวย อันใด ได้รับความ ความรู้สึกชื่นชอบ อย่างมากใน บ้านเมือง
    ไทย ซึ่งมีลักษณะ กับ ตั๋ว ลอตเตอรี่ทั่วไป แต่มี ข้อแตกต่าง ในด้านการเลือก
    ตัวเลขใด และช่องทาง
    ในการ จองซื้อ จัดจำหน่าย

    การ ใช้ หวยยี่กีนั้น ผู้ พนัน จะ เลือกสรร ตัวเลข จำนวน 2-3 เลขมงคล ซึ่ง อาจจะ เป็นตัวเลข ที่มี
    ความหมายพิเศษ หรือ เลขเด็ด ที่ เกิดขึ้น ใน ความเชื่อมั่นศรัทธา ของ ต่างๆ จากนั้นนำ เลขชุด เหล่านั้น ไปจองซื้อ ที่ ช่องทางจำหน่าย จำหน่าย ตั๋ว ยี่กี ซึ่งมัก จะ คือ
    ร้านจำหน่าย ปลีก ทั่วทั้งประเทศ ในชุมชน

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

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

    ในภาพรวม ตั๋ว ยี่กีถือเป็น
    กิจกรรมการพนัน ที่ ได้รับการนิยม อย่าง มากมาย ใน ท้องถิ่น ไทย
    แม้ว่าจะ ประกอบด้วย
    ความเสี่ยงมาก สูงมาก แต่ก็ มีผู้คนจำนวนมาก ที่ มี ความ ความสนใจอย่างยิ่ง
    และ ทำการ ลอตเตอรี่
    ยี่กีอย่าง ต่อเนื่อง ทั้ง เพื่อที่จะ มุ่งหวัง ผล ผลรางวัล ที่
    สูงมาก และ เพื่าจะ ต้องการแสวงหา ความ ความรู้สึกตื่นเต้น จาก การพนัน

    Feel free to visit my site โบนัสคาสิโนออนไลน์ [jisuzm.tv]

  9. Luciana表示:

    Five Killer Quora Answers On Window Repairs Near Me window repairs near
    me (Luciana)

  10. Danieldob表示:

    Всем привет! Подскажите, где найтиполезные статьи о недвижимости? Сейчас читаю https://universal37.ru

  11. 5 Killer Quora Answers On Cheap Online Grocery Shopping Uk cheap online grocery shopping uk

  12. 15 Facts Your Boss Would Like You To Know You’d Known About Mesothelioma
    Law Mesothelioma Claims

  13. Ernestzothe表示:

    Ronaldo de Asis Moreira https://ronaldinhogaucho.prostoprosport-br.com Brazilian footballer, played as an attacking midfielder and striker. World Champion (2002). Winner of the Golden Ball (2005). The best football player in the world according to FIFA in 2004 and 2005.

  14. comment-37781表示:

    hello!,I love your writing very a lot! percentage we
    be in contact extra about your post on AOL?
    I need a specialist on this space to unravel my problem.
    Maybe that is you! Taking a look forward to see you.

  15. Tyrell表示:

    Whats up this is kind of of off topic but I was wondering if blogs use WYSIWYG
    editors or if you have to manually code with HTML. I’m starting a
    blog soon but have no coding knowledge so I
    wanted to get advice from someone with experience. Any help would be greatly appreciated!

  16. Guide To Upvc Repairs Near Me: The Intermediate Guide For Upvc Repairs Near Me upvc Repairs near Me

  17. PatrickSox表示:

    Xavi or Xavi Quentin Sy Simons https://xavi-simons.prostoprosport-fr.com Dutch footballer, midfielder of the Paris Saint-Germain club -Germain” and the Dutch national team, playing on loan for the German club RB Leipzig.

  18. Theгe’s definately а lot tо fіnd outt about tһis issue.
    Ӏ love aall the poiints уou’ve maɗe.

    Review mʏ homеpagе :: situs judi slot

  19. 10 Tell-Tale Signs You Need To Look For A New Online Shopping Sites In Uk For
    Electronics Which supermarket is cheapest for online shopping

  20. Abbie表示:

    10 Repair Upvc Windows That Are Unexpected repairing
    upvc windows (Abbie)

  21. kraken 2fa表示:

    of course like your web-site however you have to take a look at the spelling on several of your
    posts. Many of them are rife with spelling problems and I find
    it very bothersome to inform the reality nevertheless I’ll surely come again again.

  22. netfilmcity表示:

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

  23. 9 Lessons Your Parents Teach You About Nespresso Machine Nespresso Coffee Machines (Puriagatratt.Blog.Idnes.Cz)

  24. Bunk Beds That Split Into Singles Tools To Improve Your Daily Life Bunk Beds That Split
    Into Singles Trick That Every Person Should Be Able To bunk beds That Split Into singles

  25. bokep indo表示:

    This is a topic which is close to my heart… Many thanks!
    Where are your contact details though?

發佈留言

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