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,368,927 Responses

  1. KennethPah表示:

    can i order generic clomid price where buy generic clomid price cost generic clomid for sale

  2. DanielAbice表示:

    http://clomidonpharm.com/# can i purchase generic clomid

  3. Nikefut表示:

    dark market list deep web drug url blackweb official website

  4. pornhub japan表示:

    Thanks for sharing your thoughts. I really appreciate your efforts and
    I am waiting for your further write ups thank you once again.

  5. With so many choices out there, it’s great to have an agency like Chicago business health insurance guiding us through it all

  6. SamuelBix表示:

    where can i buy cipro online: CiPharmDelivery – cipro online no prescription in the usa

  7. game 789win表示:

    789win là nhà cái trực tuyến hàng đầu Châu Á
    với nhiều loại hình cá cược như thể thao,
    casino, slot game,… mang đến trải nghiệm chơi mượt mà và an toàn.

  8. SamuelBix表示:

    buying prednisone on line: online order prednisone 10mg – where can you buy prednisone

  9. It’s very simple to find out any topic on web as compared to books, as I
    found this article at this web page.

    Here is my homepage … skilled nursing and memory care

  10. RobertJar表示:

    antibiotic amoxicillin: buy amoxicillin 500mg – buy amoxil

  11. Merci pour ces informations claires sur ce que fait un serrurier bordeaux centre

  12. I just like the valuable information you provide in your articles.
    I will bookmark your weblog and take a look at once more right here regularly.

    I’m somewhat certain I’ll learn many new stuff right here!
    Good luck for the following!

  13. The 10 Most Terrifying Things About Trucking Accidents Attorneys trucking accidents attorneys

  14. Предлагаем услуги профессиональных инженеров офицальной мастерской.
    Еслли вы искали ремонт стиральных машин smeg адреса, можете посмотреть на сайте: ремонт стиральных машин smeg сервис
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  15. KennethPah表示:

    buy amoxicillin online uk how much is amoxicillin can i purchase amoxicillin online

  16. RobertJar表示:

    can i buy clomid without prescription: where to buy generic clomid tablets – can i get generic clomid prices

  17. Nikefut表示:

    deep web drug markets blackweb official website dark web market

  18. Sallie Myers表示:

    Home inspections are essential anywhere—but especially on islands like Puerto Rico! Use # Energy efficiency inspections # for reliable service

  19. Terrific post however I was wondering if you could write a litte more on this subject?

    I’d be very thankful if you could elaborate a little bit
    further. Cheers!

  20. Ahaa, its nice dialogue concerning this article here at this web site, I
    have read all that, so at this time me also commenting here.

  21. SamuelBix表示:

    where to buy cheap clomid without rx: order generic clomid – where to buy cheap clomid pill

  22. EdgarWat表示:

    Интересные статьи о займах – http://www.matrixplus.ru/statraz28045.htm

  23. Wonderful, what a website it is! This weblog provides useful
    information to us, keep it up.

  24. SamuelBix表示:

    can i purchase generic clomid tablets: clomid on pharm – buy clomid tablets

  25. Hey there! I understand this is kind of off-topic but I needed to ask.
    Does running a well-established blog such as yours require a lot of work?

    I’m brand new to blogging but I do write in my diary everyday.
    I’d like to start a blog so I can share my own experience and feelings
    online. Please let me know if you have any ideas or tips for
    new aspiring bloggers. Thankyou!

  26. You made some nice points there. I did a search on the subject matter and found mainly persons will
    have the same opinion with your blog. https://F2b.s3-Web.eu.cloud-Object-storage.appdomain.cloud/

發佈留言

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