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,168,709 Responses

  1. What’s The Job Market For Private ADHD Titration UK Professionals?
    Private Adhd Titration

  2. Willardvah表示:

    Neymar da Silva Santos Junior https://neymar.prostoprosport-br.com e um futebolista brasileiro que atua como atacante, ponta e atacante. meio-campista do clube saudita Al-Hilal e da selecao brasileira. Considerado um dos melhores jogadores do mundo. O maior artilheiro da historia da Selecao Brasileira.

  3. Everything is very open with a really clear explanation of the challenges.

    It was really informative. Your site is very helpful. Many thanks
    for sharing! https://newtongeniuscenter.co.kr/bbs/board.php?bo_table=free&wr_id=107793

  4. asbestos Compensation (gurkenmuseum.de) is a naturally occurring mineral that was once used extensively in the construction and manufacturing industries.
    The exposure to asbestos was frequently associated with serious and even life-threatening diseases.

  5. Especially those that use corsets and basques for the first time will
    certainly fear about this matter.

  6. Hi! This is kind of off topic but I need some help from an established blog. Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about making my own but I’m not sure where to begin. Do you have any tips or suggestions? Cheers

  7. 9 . What Your Parents Taught You About Amazon Online Shopping Clothes Uk Amazon Online Shopping Clothes Uk

  8. There’s certainly a lot to know about this topic. I like all of the points you’ve made.

  9. 15 Things You Didn’t Know About Cerebral Palsy Lawyers cerebral Palsy Attorneys

  10. Повышайте свои шансы на победу в 1win казино
    1win зеркало 1win зеркало .

  11. a knowledge base isn’t just a static repository; it’s a dynamic tool that empowers your team and customers alike.

  12. 257634表示:

    The Largest Issue That Comes With Windows Repairs Near Me, And How You Can Repair It 257634

  13. Upvc Windows Near Me Tools To Ease Your Daily Lifethe One Upvc Windows Near Me Trick That Should Be Used By Everyone Know Upvc windows near me

  14. Guide To Upvc Repairs Near Me: The Intermediate Guide For Upvc Repairs Near Me upvc repairs Near me

  15. Thad表示:

    With havin so much content do you ever run into any problems of plagorism or copyright infringement?
    My site has a lot of exclusive content I’ve either written myself or outsourced but it appears a lot
    of it is popping it up all over the internet without my agreement.
    Do you know any solutions to help prevent content from being stolen? I’d definitely appreciate
    it.

  16. See What Amazon Uk Online Shopping Clothes Tricks The Celebs Are Utilizing Amazon uk Online shopping clothes

  17. We support the top quality in our products.

    my blog :: is lion&39;s mane legit (Finlay#)

  18. The Most Convincing Evidence That You Need Anxiety Disorder Treatments At Home extreme anxiety treatment

  19. Hey! Someone in my Facebook group shared this website with us so I
    came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting
    this to my followers! Outstanding blog and superb design.

發佈留言

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