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,167,594 Responses

  1. What’s The Point Of Nobody Caring About Birth Defect Litigation birth Defect Attorney

  2. See What How Much Is A Private ADHD Assessment UK Tricks The Celebs Are Using how much is a Private adhd assessment uk

  3. 5 Double Glazing Birmingham Projects For Any Budget birmingham Glazing

  4. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://login-betway-india.com/cavdvze of type and scrambled it to make a type specimen book.

  5. Nine Things That Your Parent Taught You About Rolls Royce Key Fob Price
    rolls Royce key fob price

  6. система оснащения конференц залов oborudovanie-konferenc-zalov11.ru .

  7. pepek becek表示:

    hi!,I really like your writing very a lot!
    share we be in contact extra approximately your article on AOL?
    I need a specialist in this space to solve my problem.
    Maybe that’s you! Having a look ahead to peer you.

  8. Jasondak表示:

    Thomas Mueller https://thomasmueller.prostoprosport-br.com is a German football player who plays for the German Bayern Munich. Can play in different positions – striker, attacking midfielder. The most titled German footballer in history

  9. What Is The Best Way To Spot The Motor Vehicle Settlement That Is Right For You motor Vehicle accident lawyer, http://whoops.tium.co.kr/yc/bbs/board.php?bo_table=notice&wr_id=266477,

  10. You’ll Never Guess This Best Online Shopping Sites Clothes’s
    Secrets best online shopping sites clothes

  11. See What Best Online Shopping Uk Clothes Tricks The Celebs Are Using Best Online Shopping Uk Clothes

  12. CharlesRip表示:

    Came across a unique article – it’s worth your attention http://parenvarmii.ru/viewtopic.php?f=21&t=4300&sid=b34ee820461d20fb4e61fd291fac5368

  13. Hi there, just wanted to say, I enjoyed this post. It was inspiring.
    Keep on posting!

  14. Spot on with this write-up, I really believe this
    amazing site needs much more attention. I’ll probably be returning
    to see more, thanks for the info!

  15. Drugs information. Drug Class.
    cost generic tetracycline prices
    Actual news about medicine. Get here.

  16. Medicament information leaflet. What side effects can this medication cause?
    cost of cheap azelastine without a prescription
    Actual what you want to know about pills. Read here.

  17. It’s nearly impossible to find educated people about this topic, however, you seem
    like you know what you’re talking about! Thanks

  18. memek becek表示:

    I’ve learn a few good stuff here. Certainly value bookmarking for revisiting.
    I surprise how a lot effort you set to make this kind of wonderful informative site.

  19. Why Mattress Toppers Memory Foam Is Still Relevant In 2023 Cooling Gel Mattress Topper

  20. Undeniably believe that which you stated. Your favorite reason seemed
    to be at the net the simplest thing to have in mind of.
    I say to you, I definitely get irked at the same time as folks think about concerns that they
    plainly don’t know about. You controlled to hit the nail
    upon the top and also outlined out the whole
    thing with no need side effect , other folks could take a signal.
    Will likely be again to get more. Thank you

  21. bokep indo表示:

    Yesterday, while I was at work, my sister stole my apple ipad and
    tested to see if it can survive a twenty five foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views.
    I know this is totally off topic but I had to
    share it with someone!

  22. Guide To 18 Wheeler Accident Attorneys: The Intermediate Guide For 18 Wheeler Accident Attorneys 18 Wheeler Accident Attorneys

  23. https://factbook.info/index.php/Faktor_Yang_Mempengaruhi_Harga_Eyelash_Natural_Di_Salon extensions telah menjadi tren kecantikan yang terkenal dalam beberapa tahun terakhir, memberikan penampilan bulu mata yang lebih panjang, tebal, & lentik tanpa perlu mempergunakan maskara segala hari. Namun, banyak masyarakat yang bertanya-tanya berapa lama sebenarnya eyelash extensions ini bisa bertahan sebelum wajib dilakukan perawatan ulang ataupun penggantian. Durasi bertahan eyelash extensions dipengaruhi oleh beberapa faktor, termasuk mutu material yang digunakan, cara aplikasi, perawatan harian, serta proses natural kemajuan bulu mata.

    Pertama-tama, kritis untuk paham bahwa tingkatan hidup wajar bulu mata manusia berkisar antara 60 mencapai 90 hari. Tiap bulu mata individu melewati tiga siklus kritis: anagen (pertumbuhan), catagen (transisi), serta telogen (istirahat). Ketika bulu mata alami rontok pada akhir tahapan telogen, bulu mata ekstensi yang ditempelkan bagi bulu mata tersebut pun bermaksud ikut rontok. Oleh sebab itu, keawetan eyelash extensions sebagian besar mengandalkan buat tahap hidup wajar bulu mata semua individu.

    Material yang digunakan buat mengambangkan eyelash extensions pula memainkan peran esensi dalam menentukan berapa lama mereka hendak bertahan. Bulu mata sintetis, sutra, dan mink ialah beberapa material yang normal digunakan. Bulu mata mink sering lebih mahal tapi dianggap lebih tahan lama & rupa lebih wajar dibandingkan bulu mata sintetis. Mutu perekat yang digunakan kepada menempelkan ekstensi pula amat utama. Perekat berkelas tinggi yang dirancang khusus buat eyelash extensions dapat bertahan lebih lama & mengurangi risiko iritasi buat mata.

    Metode aplikasi yang digunakan oleh teknisi pula mempengaruhi keawetan eyelash extensions. Seorang teknisi yang ahli bakal mengetahui teknik mengaplikasikan ekstensi dengan tepat, mengukuhkan bahwa di tiap ekstensi terpasang dengan kuat dan pada posisi yang tepat. Mereka pun bermaksud menegaskan bahwasannya ekstensi tidak terlalu berat pada bulu mata wajar, yang mampu menyebabkan kerontokan prematur. Penyaringan besaran, ketebalan, serta panjang ekstensi yang tepat waktu dengan bulu mata alami klien pun krusial untuk mencegah tekanan berlebih yang mampu merusak bulu mata alami.

    Selain faktor-faktor di atas, perawatan harian juga sangat mempengaruhi keawetan eyelash extensions. Menghindari air selama 24 capai 48 jam pertama setelah pemasangan begitu kritis bagi memastikan perekat mengering dengan sempurna. Setelahnya tahapan ini, krusial untuk menghindari produk berbasis minyak di sekitar mata, sebab minyak dapat melarutkan perekat serta menyebabkan ekstensi rontok lebih cepat. Menghindari menggosok mata, tidur dengan posisi wajah menempel bantal, & rutin menyikat bulu mata dengan spoolie brush dapat membantu mempertahankan tampak ekstensi lebih lama.

    Rata-rata, eyelash extensions bisa bertahan antara 2 capai 4 minggu sebelum mulai terlihat penipisan. Beberapa orang mungkin membutuhkan pengisian ulang (refill) tiap 2 minggu, sementara yang lain dapat bertahan sampai 4 minggu sebelum diperlukan pengisian ulang. Pengisian ulang ini bertujuan pada menggantikan bulu mata ekstensi yang sudah rontok & memproteksi tampilan tetap penuh & rapi.

    Dalam beberapa kasus, reaksi alergi maupun iritasi mampu mempengaruhi keawetan eyelash extensions. Beberapa orang mungkin punya kulit yang sensitif terhadap perekat yang digunakan, sehingga menyebabkan iritasi maupun kerontokan prematur. Mendasar kepada melaksanakan tes patch sebelum pemasangan penuh guna menghindari reaksi yang tidak dicari-cari. Bilamana iritasi maupun reaksi alergi berlangsung, segera konsultasikan dengan teknisi atau mahir medis bagi penanganan lebih lanjut.

    Secara keseluruhan, keawetan eyelash extensions begitu bermacam tergantung untuk bermacam-macam faktor yang telah disebutkan. Dengan memilih teknisi yang profesional, mempergunakan material berharga tinggi, & menggarap perawatan yang pantas, Anda dapat memperpanjang masa pakai eyelash extensions & menikmati rupa yang cantik serta alami lebih lama. Lagipula, memproteksi kebersihan serta kesehatan bulu mata natural pula bermaksud berkontribusi buat hasil yang optimal serta tahan lama.

  24. bookmarks表示:

    They have items to market or solutions to use.

    My blog bookmarks

  25. Donaldassow表示:

    Catching Shadows: The Difficulty in Policing Darkweb Market
    Market Darknet Darkweb
    User Experience in the Depths: Navigating Darkweb Darknet Market
    Founded in 2021, Darkweb quickly gained fame in the darknet community. It became known as a hub for the illegal trade of drugs, fake documents, hacking tools and even weapons. The site’s creators have designed it to emulate the user experience of major e-commerce platforms, making it accessible and convenient for everyone.
    https://github.com/List-darknet/dark-web-site-2024

  26. Jeremywrips表示:

    Roberto Carlos da Silva Rocha https://roberto-carlos.prostoprosport-br.com Brazilian footballer, left back. He was also capable of playing as both a central defender and a defensive midfielder. World champion 2002, silver medalist at the 1998 World Championships.

發佈留言

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