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

  1. This piece of writing is truly a nice one it helps new web
    users, who are wishing for blogging.

  2. I am truly glad to glance at this weblog posts which carries lots of valuable facts,
    thanks for providing these information.

  3. Vada表示:

    See What Great Green Macaw Tricks The Celebs Are Using great green macaw [Vada]

  4. Nikefut表示:

    dark market list deep dark web dark web market list

  5. AngeloMax表示:

    AI-driven businesses are usually becoming increasingly attainable to solo business people, offering numerous in order to innovate and level with minimal sources. One promising concept is AI-based content creation, where individuals could leverage tools to create blogs, social media posts, or even video scripts, getting together with the growing need for digital content.

    Another viable option is developing AI-powered chatbots for smaller businesses. These bots will manage customer inquiries, control bookings, and improve user experience, permitting entrepreneurs to provide scalable services with out a large team.

    https://www.techmagazines.net/ai-business-ideas-that-one-person-can-start/

    Finally, AI-driven data analysis services happen to be in high requirement. By providing personalized observations and predictive stats, one individual can aid businesses make informed decisions based upon their own data, positioning themselves as an essential asset in any industry.

  6. Fannie Walsh表示:

    Thanks to the resources shared by interstate moving companies #

  7. Hello just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Opera.
    I’m not sure if this is a formatting issue or something to do with
    web browser compatibility but I figured I’d post to let you know.
    The design and style look great though! Hope you get the
    issue solved soon. Thanks

  8. C.daum7.net表示:

    Hey! I know this is kind of off topic but I was wondering which blog platform are you using for
    this site? I’m getting tired of WordPress because I’ve had problems with hackers and
    I’m looking at alternatives for another platform. I would be awesome if you could point me in the
    direction of a good platform.

    Feel free to surf to my web blog … C.daum7.net

  9. ThelmaFange表示:

    Here you will find a unique assortment rare coins from the whole planet. We offer both modern and historical specimens that will become real finds for lovers of numismatics.
    For more information, follow the link [url=https://numizmatexpert.ru/]купить монеты ссср [/url]
    Our team of experts carefully selects each coin, ensuring authenticity.

  10. KennethPah表示:

    amoxicillin 500mg capsule cost Amox Star amoxicillin azithromycin

  11. PuffyGotBuffy is always here to support you with any questions about individual training. Our devoted group is prepared to help you, ensuring you have the information you need to achieve your fitness goals personal trainers

  12. KennethPah表示:

    buy prednisone nz Predni Best how to buy prednisone

  13. Jugabet_poel表示:

    Jugabet apuestas deportivas Jugabet apuestas deportivas .

  14. WOW just what I was searching for. Came here by searching for 12 week testosterone enanthate
    results

  15. สายไวน์คนไหนอยู่ที่นี่บ้าง ช่วยแชร์ประสบการณ์ซื้อไวน์หน่อยค่ะ ตอนนี้กำลังสนใจไวน์แดงและไวน์ขาวมาก
    ช่วยแนะนำแหล่งซื้อไวน์หน่อยค่ะ การเลือกไวน์ที่เหมาะสมไม่ใช่เรื่องง่ายเลย รอคำตอบจากทุกคนอยู่นะ

    เว็บไซต์นี้มีข้อมูลที่น่าสนใจมาก ติดตามเว็บนี้แล้วรู้สึกว่ามีความรู้เรื่องไวน์เพิ่มขึ้น ใครสนใจเรื่องไวน์แนะนำให้ติดตามเว็บนี้เลยค่ะ
    สุดท้ายนี้ขอให้ทุกคนเจอไวน์ที่ชอบนะคะ จะติดตามเว็บนี้ต่อไปเรื่อยๆ
    ค่ะ ขอบคุณมากค่ะ!

    my page … ไวน์ขายส่ง

  16. KennethPah表示:

    prednisone cost in india PredniBest over the counter prednisone medicine

  17. SamuelBix表示:

    п»їcipro generic: buy cipro without rx – cipro

  18. My family has benefited greatly from hiring a dedicated best personal injury attorney # after our car accident—highly

  19. RobertJar表示:

    no prescription prednisone canadian pharmacy: Predni Best – buy prednisone without prescription paypal

  20. RobertJar表示:

    can you get cheap clomid without prescription: cost of cheap clomid no prescription – can i purchase generic clomid prices

  21. KennethPah表示:

    where can i buy cipro online cipro 500mg best prices ciprofloxacin generic

  22. This post is really a fastidious one it hslps nnew the web people, who are wishing for blogging. https://goldbullion.z20.web.core.windows.net/

  23. 20 Trailblazers Setting The Standard In Built In Microwave Oven white Built in microwave oven

發佈留言

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