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,466,909 Responses

  1. seobiawak表示:

    Terima kasih atas informasinya! Kebetulan saya menemukan pembahasan serupa di KADEKTOTO SLOT GACOR.

  2. click hear表示:

    Hello Dear, are you really visiting this web site
    daily, if so after that you will without doubt get good knowledge.

  3. seobiawak表示:

    Pembahasan yang bagus! Untuk informasi tambahan, Anda bisa mengunjungi KADEKTOTO SLOT GACOR.

  4. Ralph Huff表示:

    Thanks for the great information. More at roofers greenville sc

  5. seobiawak表示:

    Pembahasan yang bagus! Untuk informasi tambahan, Anda bisa mengunjungi KADEKTOTO SLOT GACOR.

  6. 시흥호빠表示:

    시흥쪽에도 여성분들이 놀 곳이 있죠. 바로 저희 시흥호빠입니다

  7. BradleyEdino表示:

    sweet bonanza kazanma saatleri sweet bonanza kazanma saatleri sweet bonanza guncel

  8. Pretty section of content. I just stumbled upon your site and in accession capital to
    assert that I acquire actually enjoyed account your blog posts.

    Anyway I’ll be subscribing to your feeds and even I achievement
    you access consistently fast.

  9. seobiawak表示:

    Pembahasan yang bagus! Untuk informasi tambahan, Anda bisa mengunjungi KADEKTOTO SLOT GACOR.

  10. Wow, marvelous blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your site is magnificent,
    as well as the content!

  11. Marc Garcia表示:

    Kudos to the staff at Commercial moving services for being so helpful throughout my entire moving

  12. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# yeni deneme bonusu veren siteler

  13. seobiawak表示:

    Terima kasih atas informasinya! Kebetulan saya menemukan pembahasan serupa di KADEKTOTO SLOT GACOR.

  14. Iola表示:

    In many cases, a numbing representative is put on the area of the lesion.

  15. seobiawak表示:

    Pembahasan yang bagus! Untuk informasi tambahan, Anda bisa mengunjungi KADEKTOTO SLOT GACOR.

  16. Latesha表示:

    A number of techniques are available to securely and efficiently eliminate skin tags.

  17. Hydrolysates with pepsin or with aminopeptidase were administered to obese Zucker rats for 12
    weeks via their drinking water.

  18. TimothyTow表示:

    After inputting my measurements and preferencesтАЪ I dove headfirst into the exciting world of layout experimentation. KitchenCraft Pro offered a plethora of optionsтАЪ from the classic galley style to the more spacious L-shape and U-shape designs. I started by exploring the galley layoutтАЪ digitally dragging and dropping cabinets and appliances to visualize the workflow. I quickly realized it wouldn’t suit my needs тАУ the limited counter space was a major drawback. NextтАЪ I experimented with an L-shape configurationтАЪ strategically placing the sinkтАЪ stoveтАЪ and refrigerator to form an efficient work triangle. This felt more promising. I meticulously adjusted the placement of islands and peninsulasтАЪ carefully considering their impact on traffic flow and overall spaciousness. The software’s intuitive drag-and-drop functionality made this process remarkably easy and enjoyable. I even tried a daring U-shaped designтАЪ envisioning a grandтАЪ chef-like workspace. While initially appealingтАЪ I found it slightly cramped for my needs. The software allowed me to easily switch between different layoutsтАЪ instantly visualizing the impact of each change. I spent hours tweaking the arrangement of cabinetsтАЪ appliancesтАЪ and workspacesтАЪ constantly evaluating the practicality and aesthetic appeal of each configuration; I played around with different cabinet depths and widthsтАЪ experimenting with the placement of pantry units and maximizing storage space. I even considered adding a breakfast bar to one of the layoutsтАЪ envisioning family gatherings around it. The ability to instantly see the results of my adjustments was invaluableтАЪ allowing me to make informed decisions based on both functionality and personal preference. It was a truly iterative processтАЪ refining and perfecting the design until I found the perfect balance between form and function. Through this experimentationтАЪ I discovered that the L-shape layoutтАЪ with a cleverly positioned islandтАЪ provided the optimal blend of efficiency and spaciousness for my kitchen.
    I embarked on a thrilling kitchen redesignтАЪ leveraging free online tools. My goal? A stunningтАЪ functional space without breaking the bank. I spent hours exploring various platformsтАЪ comparing features and ease of use. The process felt surprisingly intuitive. I was amazed by how quickly I could visualize different layouts and experiment with various appliances and cabinetry. It was a fun and empowering experienceтАЪ proving that achieving a dream kitchen doesn’t require a hefty budget!

    https://instepnews.com/construction/my-220-volt-kitchen-appliance-upgrade/

    After inputting my measurements and preferencesтАЪ I dove headfirst into the exciting world of layout experimentation. KitchenCraft Pro offered a plethora of optionsтАЪ from the classic galley style to the more spacious L-shape and U-shape designs. I started by exploring the galley layoutтАЪ digitally dragging and dropping cabinets and appliances to visualize the workflow. I quickly realized it wouldn’t suit my needs тАУ the limited counter space was a major drawback. NextтАЪ I experimented with an L-shape configurationтАЪ strategically placing the sinkтАЪ stoveтАЪ and refrigerator to form an efficient work triangle. This felt more promising. I meticulously adjusted the placement of islands and peninsulasтАЪ carefully considering their impact on traffic flow and overall spaciousness. The software’s intuitive drag-and-drop functionality made this process remarkably easy and enjoyable. I even tried a daring U-shaped designтАЪ envisioning a grandтАЪ chef-like workspace. While initially appealingтАЪ I found it slightly cramped for my needs. The software allowed me to easily switch between different layoutsтАЪ instantly visualizing the impact of each change. I spent hours tweaking the arrangement of cabinetsтАЪ appliancesтАЪ and workspacesтАЪ constantly evaluating the practicality and aesthetic appeal of each configuration; I played around with different cabinet depths and widthsтАЪ experimenting with the placement of pantry units and maximizing storage space. I even considered adding a breakfast bar to one of the layoutsтАЪ envisioning family gatherings around it. The ability to instantly see the results of my adjustments was invaluableтАЪ allowing me to make informed decisions based on both functionality and personal preference. It was a truly iterative processтАЪ refining and perfecting the design until I found the perfect balance between form and function. Through this experimentationтАЪ I discovered that the L-shape layoutтАЪ with a cleverly positioned islandтАЪ provided the optimal blend of efficiency and spaciousness for my kitchen.
    With my chosen design toolтАЪ KitchenCraft ProтАЪ selectedтАЪ the next step was inputting my kitchen’s precise measurements. This proved surprisingly straightforward. The software guided me through a step-by-step processтАЪ prompting me to enter the length and width of each wallтАЪ the location of windows and doorsтАЪ and the dimensions of existing fixtures like sinks and ovens. I found the interface incredibly intuitiveтАЪ with clear instructions and helpful visual aids. I even appreciated the option to upload a floor plan image to assist with the process. InitiallyтАЪ I struggled a bit with accurately measuring some awkwardly shaped alcovesтАЪ but the platform’s customer support chat was incredibly helpfulтАЪ providing quick and efficient guidance. Once the dimensions were finalizedтАЪ I began to personalize the design. This is where the fun really started! I selected my preferred cabinet stylesтАЪ choosing from a comprehensive library of options ranging from sleek modern designs to more traditionalтАЪ rustic styles. I meticulously chose the colorsтАЪ finishesтАЪ and countertop materialsтАЪ experimenting with different combinations to find the perfect aesthetic. I even incorporated my existing appliancesтАЪ ensuring they fit seamlessly into the new layout. The software allowed for precise placement of every elementтАЪ down to the smallest detail. I spent hours tweaking and refiningтАЪ meticulously considering factors such as workflow efficiency and aesthetic harmony. The process felt like playing a sophisticated digital puzzleтАЪ and the satisfaction of seeing my vision slowly take shape on the screen was incredibly rewarding. I discovered that the software’s flexibility allowed me to explore different layouts and experiment with various design choices without any commitmentтАЪ a feature I truly appreciated. It was a crucial step in ensuring my final design would be both functional and visually appealing.
    Refining the Design and Adding Details
    With a satisfying L-shaped layout as my baseтАЪ I delved into the finer detailsтАЪ transforming my digital sketch into a truly personalized design. I meticulously selected cabinet styles from the extensive library offered by the softwareтАЪ opting for sleekтАЪ modern shaker-style cabinets in a warmтАЪ honey-toned oak finish. The program allowed me to visualize these choices in real-timeтАЪ rotating the cabinets to ensure they complemented the overall aesthetic. NextтАЪ I chose countertops. I initially considered graniteтАЪ but the virtual rendering showed it clashing slightly with the cabinet color. I then experimented with quartzтАЪ settling on a beautifulтАЪ light-grey option that beautifully contrasted with the oak. The software’s realistic rendering capabilities were invaluable here. I spent a considerable amount of time tweaking the backsplashтАЪ finally settling on a subway tile pattern in a soft whiteтАЪ which provided a clean and classic look. Adding lighting was another key step. I strategically placed recessed lighting above the countertops and pendant lights above the islandтАЪ creating a warm and inviting ambiance. I even experimented with different flooring optionsтАЪ virtually laying down various types of tile and hardwood to see how they interacted with the rest of the design. I was particularly pleased with a light-colored oak wood floor that tied in seamlessly with the cabinetry. Beyond the major elementsтАЪ I focused on the smaller details. I carefully selected hardwareтАЪ opting for brushed nickel pulls and knobs that added a touch of sophistication. I also integrated various appliancesтАЪ choosing models based on their size and aestheticsтАЪ ensuring they fit harmoniously within the design. The software allowed me to fine-tune every aspectтАЪ from the placement of outlets to the positioning of spice racksтАЪ ensuring a highly functional and visually appealing kitchen. It was a truly rewarding processтАЪ seeing my initial concept evolve into a cohesive and detailed designтАЪ ready to be shared and potentially implemented in the real world. The level of customization was impressiveтАЪ allowing me to create a space that was uniquely mine.
    Inputting My Measurements and Preferences

  19. Greetings, I do think your web site could possibly be having web browser compatibility issues.
    Whenever I look at your web site in Safari,
    it looks fine however, when opening in Internet Explorer, it
    has some overlapping issues. I merely wanted to provide you with a quick heads up!
    Apart from that, fantastic blog!

  20. From here, they can maintain the online will and use it the same way they would any various other.

  21. I every time used to study paragraph in news papers but now as
    I am a user of internet therefore from now I am using net for content,
    thanks to web.

  22. Charlotte Bailey表示:

    This is quite enlightening. Check out متن روز پدر برای استوری for more

  23. 호빠 정말 많죠? 저희 경기도호빠만큼 많은 곳 없죠?

  24. Melvin表示:

    Federal and state regulations and laws are intricate and are subject to transform.

  25. seobiawak表示:

    Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.

  26. I like the valuable info you provide for your articles.
    I’ll bookmark your weblog and take a look at
    once more here frequently. I am slightly sure I will be informed many new stuff proper here!
    Good luck for the following!

  27. FrankEmine表示:

    az parayla cok kazandiran slot oyunlar?: slot oyunlar? puf noktalar? – slot oyunlar? puf noktalar?

  28. Dean Clayton表示:

    I recently visited a chiropractor in Lakewood, WA Lakewood Chiropractor

發佈留言

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