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,147,900 Responses

  1. slot payouts表示:

    14 Businesses Doing A Great Job At Slot Rewards slot payouts

  2. 3D Slots表示:

    Five Killer Quora Answers To 3d Slots 3D Slots

  3. Watch Out: How Semi Truck Legal Is Taking Over And What To Do About It Semi Truck Accident Lawsuits

  4. window repair表示:

    Window Repairs Tools To Improve Your Daily Life Window Repairs Trick That
    Everybody Should Learn window repair

  5. Darci表示:

    Attractive part of content. I just stumbled upon your blog and in accession capital to
    assert that I get in fact loved account your weblog posts.
    Any way I’ll be subscribing on your feeds and even I fulfillment you get
    admission to consistently quickly.

  6. 비아그라 구매방법 의사 상담부터 온라인 구매까지
    성적 기능 장애는 남성들 사이에서 흔히 발생하는 문제 중 하나입니다. 이런 문제에 대처하기 위해 비아그라와 같은 약물이 널리 사용되고 있습니다. 그러나 비아그라는 처방전이 필요한 약물로 분류되어 있기 때문에, 올바른 절차를 따라 구매해야 합니다.

  7. jpandi.co.kr表示:

    10 Quick Tips For Window Repair Near upvc window repair near me (jpandi.co.kr)

  8. I’ve learn several good stuff here. Certainly price bookmarking for revisiting.

    I surprise how much attempt you set to create this kind of wonderful informative website.

  9. services表示:

    What Is The Reason? Double Glazed Windows Repairs Is Fast Becoming The Most Popular Trend For
    2023? services

  10. What’s The Job Market For Double Glazed Window Repairs Professionals?
    double glazed window Repairs

  11. It’s a shame you don’t have a donate button! I’d most certainly donate to this fantastic blog!
    I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account.

    I look forward to brand new updates and will talk about this site with my Facebook group.
    Talk soon!

  12. 5 Best Online Shopping Uk Clothes Projects For Any Budget Premium Serving Trays

  13. What’s up, after reading this amazing piece of writing i am also happy to share my knowledge here with colleagues.

  14. Wow many of valuable material.

  15. 10 Facts About Online Shopping Sites In Uk For Electronics That Insists On Putting You In The Best Mood Driveway Snow Stakes

  16. How To Explain Online Shopping Sites In Uk For Electronics To Your Grandparents Pyramex I-Force Safety Eyewear

  17. Vegas Slots表示:

    The 10 Most Scariest Things About Vegas Slots Vegas Slots

  18. 10 Misconceptions Your Boss Shares Concerning Injury Law
    injury law firm

  19. By this point, the samples had transformed into sludgy, slimy mixtures.

  20. The $731.1 million jackpot is the fourth biggest inn Powerball’s 28-year history and the sixth
    largest lottery jackpot ever in the United States, Powerball announced on Thursday.

    Also visit my web blog Digitalbengaltv.Com

  21. I’ve been exploring for a bit for any high quality articles or blog posts in this sort
    of area . Exploring in Yahoo I ultimately stumbled upon this web site.
    Studying this info So i am happy to express that I’ve a very good uncanny feeling I came upon just what
    I needed. I most definitely will make sure to don?t forget this web site and give it
    a look on a constant basis.

  22. Guide To Slot Competitions: The Intermediate Guide To Slot Competitions Slot competitions

  23. Excellent post. Keep posting such kind of information on your site.
    Im really impressed by it.
    Hello there, You’ve performed an excellent job.
    I’ll definitely digg it and in my view recommend to my friends.
    I’m confident they will be benefited from this site.

  24. A fascinating discussion is definitely worth comment.
    I believe that you need to publish more about this issue, it might not be a taboo matter but typically people don’t speak about such
    topics. To the next! All the best!!

  25. สโบเบ็ตเป็นเว็บพนันที่มีความเป็นที่นิยมอย่างแพร่หลายในเอเชียและทั่วทั่วโลก สโบเบ็ตเปิดให้บริการตั้งแต่ปี 2532 และได้รับใบอนุญาตจากรัฐบาลเกาหลีใต้ สโบเบ็ตมีเกมส์คาสิโนที่หลากหลาย เช่นสล็อต
    รูเล็ต แบล็คแจ็ค และอื่นๆ นอกจากนี้ยังมีการเดิมพันกีฬา
    เดิมพันบอล บาสเกตบอล และพนันกีฬาอื่นๆ อีกมากมาย สโบเบ็ตมีการให้บริการที่ปลอดภัยและดี พร้อมระบบฝากถอนที่รวดเร็วและง่าย หากคุณชื่นชอบการเล่นพนันออนไลน์ สโบเบ็ตอาจเป็นเว็บไซต์ที่คุณต้องการลองเข้าเล่นค่ะ

    My web-site … เว็บคาสิโนออนไลน์ที่มีระบบการฝาก-ถอนเงินที่รวดเร็วและปลอดภัย

  26. Qooh.Me表示:

    10 Unexpected Classic Slots Tips Mobile-Friendly Slots [Qooh.Me]

  27. You’ll Never Be Able To Figure Out This Treadmills For Home UK’s
    Secrets Treadmills for Home uk

  28. Good write-up. I absolutely love this site. Thanks!

發佈留言

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