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,163,971 Responses

  1. The vapor and flavor top quality is likewise excellent.

    Also visit my web blog – where to buy plant based meals

  2. NewULife Ranks. Welcome to my NewUlife Review.

    Have a look at my homepage; types of disability benefits uk

  3. This vaporizer has a couple of unique attributes.

    Also visit my blog post: Plant based vape near me

  4. Bookmarks表示:

    Discovering a language does not have to be costly.

    my web-site – Bookmarks

  5. Bookmarks表示:

    Allow me show you in my review of NewULife.

    My homepage – Bookmarks

  6. You do NOT intend to consume Lion’s Mane Mushrooms raw.

    Stop by my web page … is mushroom coffee Safe

  7. 5 Killer Quora Answers On Uk Online Shoe Shopping
    Websites Uk online shoe shopping websites

  8. Bookmarks表示:

    What is New U Life SOMADERM Transdermal Gel.

    Have a look at my web blog – Bookmarks

  9. celebrex drug表示:

    Medicines information sheet. Drug Class.
    celebrex drug
    Best what you want to know about drugs. Read here.

  10. Both mushrooms products are immune building
    contractors.

    Here is my blog post … mellow mushroom gatlinburg reviews

  11. Zack Foxworth表示:

    15 Reasons Not To Ignore Folding Treadmill Zack Foxworth

  12. Fantastic beat ! I wish to apprentice at the same time as
    you amend your web site, how can i subscribe for a weblog web site?

    The account helped me a appropriate deal. I have been tiny bit familiar of this your broadcast offered
    vivid clear idea.

    Feel free to surf to my homepage: best seo services in jalandhar

  13. Bookmarks表示:

    What is New U Life SOMADERM Transdermal Gel.

    Here is my web-site … Bookmarks

  14. Proofing is the very last phase in modifying a publication.

    Visit my blog :: book writing services

  15. Experienced and specialist motorists.

    Feel free to surf to my page … party bus perth prices

  16. In this instance, the life insurance company’s cash.

    Feel free to surf to my web page; what causes rotting food

  17. Medicine information leaflet. Generic Name.
    can i get cozaar no prescription
    Best information about meds. Read information here.

  18. The IQ is the most effective completely dry natural
    herb vape I have actually tried.

    Stop by my blog canadian plant based meat companies

  19. Bookmarks表示:

    USP human growth hormonal agent (somatropin).

    My web blog: Bookmarks

  20. Bookmarks表示:

    Allow me reveal you in my review of NewULife.

    Here is my homepage … Bookmarks

  21. Medicines information. Long-Term Effects.
    can i purchase cheap tamoxifen without a prescription
    Everything what you want to know about medicament. Read here.

  22. Bookmarks表示:

    XYGENYX, a licensing company for FDA-registered items.

    Also visit my webpage :: Bookmarks

  23. In this situation, the life insurance policy company’s
    loan.

    Look at my website ai picture generator reddit

  24. Someone essentially lend a hand to make critically articles I might state.
    This is the first time I frequented your website page and thus far?

    I surprised with the analysis you made to create this particular publish
    amazing. Magnificent job!

    Stop by my blog zib digital – best seo company in ahmedabad photos

  25. 10 Healthy Wall Mount Fireplace Habits lynnbolvin.top

  26. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://cmedia-design.dk/jotjnom of type and scrambled it to make a type specimen book.

  27. Everything is very open with a precise description of the challenges.
    It was definitely informative. Your website is very helpful.

    Thanks for sharing!

發佈留言

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