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,370 Responses

  1. See What Railroad Injuries Lawsuit Tricks The Celebs
    Are Utilizing railroad injuries lawsuit

  2. 913875表示:

    Five Essential Qualities Customers Are Searching For In Every Stove 913875

  3. porn site表示:

    Hello I am so happy I found your blog, I really found you by error, while I was
    researching on Bing for something else, Nonetheless I am here now and would just like to say thanks for a remarkable post and a all round enjoyable blog (I also love the theme/design), I don’t have time to read it all at the minute but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the awesome b.

  4. Beware Of These “Trends” About Best Coffee Machines http://www.4182051.xyz

  5. kaymell.uk表示:

    The Top Reasons Why People Succeed In The Slots Industry kaymell.uk

  6. ibio.com.ua表示:

    як її перенести? Що відчувають при
    всьому цьому рідні та близькі?
    тоді вам – треба за цією адресою!

    Also visit my blog post :: ibio.com.ua

  7. What’s The Job Market For Online Shopping Uk For Clothes
    Professionals Like? online shopping uk for clothes

  8. lawyer表示:

    The Reasons Act Fela Is More Difficult Than You Imagine lawyer

  9. Tommyvom表示:

    Hello there! comment-avatar great site.

  10. cassylawn.top表示:

    10 Quick Tips About Asbestos Exposure Attorney cassylawn.top

  11. What’s The Current Job Market For Upvc Repairs Near Me Professionals Like?
    Upvc Repairs Near Me

  12. Asbestos may be exposed in many different ways. The majority of asbestos case (http://www.dinasboatyard.com)-related claims relate to occupational
    exposure.

  13. Быстрая регистрация и простая верификация в 1win казино
    1win 1win .

  14. bactrim sale表示:

    Drugs information sheet. Long-Term Effects.
    bactrim sale
    Best about medicament. Get here.

  15. The 9 Things Your Parents Taught You About Online Famous Shopping Sites online Famous shopping sites

  16. Drugs information. Generic Name.
    cialis soft generic
    Everything news about drugs. Get now.

  17. 7 Things About Mesothelioma Law Firm You’ll Kick Yourself For Not Knowing mesothelioma Settlement – 50.Staikudrik.com,

  18. Drug information for patients. Drug Class.
    get generic trileptal prices
    All trends of medicament. Get information here.

  19. 1win表示:

    Играйте в любимые игры с мобильного устройства в 1win казино
    1win официальный сайт 1win официальный сайт .

  20. You can take Hunter Emphasis with other supplements.

    My website :: review mushroom head air cushion Cc cream

  21. elsycrays.top表示:

    5 Reasons To Consider Being An Online Carlocksmith And
    5 Reasons To Not elsycrays.top

  22. Nice blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would really make my blog shine.
    Please let me know where you got your design. Thank you

    Have a look at my site; is phenq safe to use

  23. aku vrtačka表示:

    Hey there! This is kind of off topic but I need some help from an established blog.
    Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty fast.

    I’m thinking about creating my own but I’m not sure where to start.
    Do you have any points or suggestions? Thanks

  24. Elenaenegrygeale表示:

    The website https://search-groups.com/ likely offers a lot of useful information and life hacks because it targets a broad audience interested in practical and effective solutions for everyday tasks. These types of tips and tricks tend to attract attention and engagement.

  25. Sex Content表示:

    I like the helpful information you provide in your articles.
    I’ll bookmark your weblog and check again here regularly. I am quite certain I will
    learn lots of new stuff right here! Best of luck for the next!

  26. ConradTum表示:

    Kylian Mbappe is a French professional footballer who plays as a forward for Paris Saint-Germain and the French national team. Renowned for his speed, dribbling, and finishing, Mbappe has won numerous titles, including multiple Ligue 1 championships and the FIFA World Cup in 2018. Find out more about him here – https://kylianmbappe.paris-saint-germain-ar.com/

發佈留言

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