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,143,061 Responses

  1. Kia Key Fob Replacement Tips That Will Change Your
    Life How To Open Kia Car Key

  2. webpage表示:

    I am really happy to read this weblog posts which consists of tons of helpful data, thanks for providing such data.

  3. Fantastic beat ! I would like to apprentice while
    you amend your website, how can i subscribe for a blog site?
    The account aided me a acceptable deal. I had been a little bit acquainted of this your
    broadcast provided bright clear idea

  4. The Biggest Problem With Online Shopping Sites For Clothes, And How You Can Fix
    It Us Online Shopping Sites For Clothes

  5. How To Outsmart Your Boss On Truck Accident Truck accident attorney

  6. viagra表示:

    Every weekend i used to pay a quick visit this site,
    because i wish for enjoyment, for the reason that this this web page conations truly
    good funny data too.

  7. pepek becek表示:

    I have read so many content regarding the blogger lovers however this piece of writing
    is in fact a nice paragraph, keep it up.

  8. urgent care表示:

    I was suggested this web site by my cousin. I’m not sure
    whether this post is written by him as nobody else know such detailed about
    my problem. You are incredible! Thanks!

  9. 5 Must-Know Online Shopping Uk Electronics Practices
    For 2023 No Gemstone Silicone Ring

  10. company表示:

    How Much Can Double Glazed Window Repairs Near Me Experts Earn? company

  11. Why We Why We Online Shopping Sites In United Kingdom (And You Should, Too!) Bx-1 Yosemite Optics

  12. I am truly thankful to the holder of this web site who has shared
    this wonderful article at here.

  13. GroverCon表示:

    Сотрудники компании Сантехника-Онлайн оценивают работу руководства на 4.1 из 5, на основании 165 оценок. Как сотрудники Сантехника-Онлайн оценивают возможности карьерного роста компании Отзывы сотрудников Сантехника Онлайн

  14. 비아그라 대체 약품의 부상
    비아그라 대체 약품의 부상: 안전하고 효과적인 옵션 탐색

  15. Medicine prescribing information. What side effects can this medication cause?
    why the cough with lisinopril
    Everything news about medicines. Get information now.

  16. Why You Should Concentrate On Making Improvements To Best Slot Machines Multi-Line Slots

  17. Sneakers表示:

    Concert Attire Stamford
    360 Fairfield Ave,
    Stamford, CT 06902, United Ꮪtates
    +12033298603
    Sneakers

  18. fun88表示:

    I do not even know how I ended up here, but I thought
    this post was great. I don’t know who you are but certainly you
    are going to a famous blogger if you are not already 😉
    Cheers!

    Feel free to surf to my web page fun88

  19. Ten Stereotypes About Low Limit Slots That Aren’t Always
    True evoplay slots strategy (http://the-good.kr)

  20. You’ll Never Guess This Online Shopping Uk Amazon’s Tricks Online Shopping Uk Amazon

  21. The 9 Things Your Parents Taught You About Examples Of Online Shopping examples Of online shopping

  22. Icam Server表示:

    I was recommended this website by my cousin. I am not
    sure whether this post is written by him as no one else know such detailed about my
    problem. You’re incredible! Thanks!

    Review my web page; Icam Server

  23. The 10 Most Terrifying Things About Shopping Online industrial arbor press (https://vimeo.com)

  24. Caranya dengan mengklik foto tersebut dan memilih opsi download yang sudah disediakan oleh Twitter.

  25. The Best Advice You Could Ever Receive On Classic Casino Slots entertaining slots (http://wx.abcvote.cn/)

發佈留言

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