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,230,838 Responses

  1. What Is Auto Locksmith Service And How To Utilize What Is Auto Locksmith
    Service And How To Use locksmith auto lockout

  2. 14 Cartoons About Online Jobs Work From Home That’ll Brighten Your
    Day Home From Work Jobs

  3. Professional Espresso Machine Tools To Ease Your Daily Lifethe One Professional Espresso Machine Trick
    That Everyone Should Know Professional Espresso Machine

  4. See What Window Repair Leeds Tricks The Celebs Are Using window Repair leeds (images.Google.cg)

  5. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 10mg prices compare

  6. If you are going for best contents like I do, only pay a visit this web page every
    day for the reason that it presents feature contents, thanks

  7. Case Opening表示:

    In Which Location To Research CS GO Case Online Case Opening

  8. Valuable data Thanks a lot.

  9. sash表示:

    20 Fun Details About Window Doctor Near Me sash

  10. small表示:

    The Top 5 Reasons People Win In The Website Search Engine Optimization Industry small

  11. Davidtes表示:

    can you buy lisinopril over the counter lisinopril 5 mg pill lisinopril 40 mg without prescription

  12. WilliamRhype表示:

    http://lisinopril.network/# purchase lisinopril 10 mg

  13. WilliamRhype表示:

    http://cytotec.club/# cytotec buy online usa

  14. 20 Myths About Automatic Folding Mobility Scooter: Busted
    automatic fold up mobility scooter

  15. Private ADHD Assessment Near Me Tools To Improve Your
    Everyday Lifethe Only Private ADHD Assessment Near Me Trick That Should Be Used By Everyone Learn private adhd assessment near me

  16. 15 Gifts For The Filtered Coffee Machine Lover In Your Life Best filter coffee machine

  17. window表示:

    Five Killer Quora Answers To Sash Window Repair window

  18. Rogelio表示:

    Looking For Inspiration? Look Up Car Diagnostics Near Me diagnostics car (Rogelio)

  19. PuffWow表示:

    I blog often and I really thank you for your content.
    The article has truly peaked my interest. I will take a note of
    your website and keep checking for new information about once a week.
    I subscribed to your RSS feed as well.

    Also visit my blog post – PuffWow

  20. This Week’s Most Popular Stories About Upvc Window Repairs Upvc Window Repairs Near Me

  21. 5 Reasons To Consider Being An Online Drive Folding Mobility Scooter Shop And 5 Reasons
    To Not self Folding mobility scooters

  22. Do You Think Scooters Folding Be The Next Supreme Ruler Of The World?
    Electric folding scooter uk

  23. 5 Killer Quora Answers To Adult ADHD Testing Adult Adhd Test

  24. Double Glazing Near Me Tools To Ease Your Daily Life Double Glazing Near Me
    Trick That Every Person Must Know double glazing Near me

  25. Davidtes表示:

    tamoxifen for breast cancer prevention tamoxifen joint pain what is tamoxifen used for

  26. Hi there, You’ve done an excellent job. I’ll certainly digg it and personally suggest to my
    friends. I am confident they’ll be benefited from this web site.

  27. WilliamRhype表示:

    http://ciprofloxacin.tech/# ciprofloxacin order online

  28. gaskeunbet表示:

    Dalam dunia judi online, sukses bukan hanya soal keberuntungan,
    tetapi juga taktik. Tips dan trik tepat sasaran dapat meningkatkan kesempatan kemenangan Anda.
    Pertama, pilih platform yang terpercaya dengan reputasi
    baik. Kedua, kenali permainannya dengan bagus dan berlatihlah mengaplikasikan akun demo sebelum
    bermain dengan uang asli. Ketiga, atur anggaran Anda dan tetaplah pada batas tersebut.
    Terakhir, manfaatkan bonus dan promosi untuk memaksimalkan poin taruhan Anda.

  29. RandallTaulp表示:

    buying cheap propecia price: generic propecia without dr prescription – buy propecia now

  30. 10 Michael Kors Ladies Handbags Tricks All Experts Recommend Michael kors handbags cream

發佈留言

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