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,228,281 Responses

  1. ebet register表示:

    Phenomenal Blog Entry
    Wow , what an thought-provoking and reflective
    content! I came across myself concurring as I read through your dissection of this essential topic .

    Your contentions were thoroughly investigated and articulated in a concise ,
    persuasive manner. I particularly valued how you were able to
    extract the fundamental intricacies and complexities at work ,
    excluding oversimplifying or overlooking the difficulties .

    This blog post has given me a lot to contemplate . You’ve undisputedly widened my
    comprehension and transformed my mindset
    in particular significant fashions .
    Appreciation for investing the effort to impart
    your knowledge on this subject . Posts like this are such a priceless contribution to the discourse .
    I anticipate seeing what other thought-provoking
    material you have in store .

    Visit my website: ebet register

  2. ckp.bedzin.pl表示:

    Wonderful article! We will be linking to this great article on our website.
    Keep up the good writing.

  3. 7 Simple Changes That Will Make A Big Difference In Your Adult Adhd
    Symptoms what are the symptoms For adhd in adults

  4. Derekfar表示:

    matadorbet bid: matadorbet – matadorbet giris

  5. ClydePab表示:

    matadorbet: matadorbet bid – matadorbet.bid

  6. An Adventure Back In Time The Conversations People Had About 3 Wheeler Pushchairs 20 Years Ago 3 wheel Pushchair sale

  7. ClydePab表示:

    ultrabet guncel: ultrabet guncel – ultrabet tr online

  8. Why We Are In Love With Anxiety Disorder (And
    You Should Also!) What Is Anxiety Panic Disorder

  9. My relatives every time say that I am wasting my time here at web, except I know I am getting
    experience every day by reading thes good articles or reviews.

  10. Do you have a spam issue on this website; I also am a blogger, and I was wondering your situation; many of us have developed
    some nice methods and we are looking to exchange techniques
    with others, be sure to shoot me an email if interested.

  11. f8bet表示:

    Hi to every one, the contents present at this website are in fact remarkable for people knowledge, well, keep up the good
    work fellows.

  12. HassanbuifS表示:

    https://ultrabet-tr.online/# ultrabet
    deneme bonusu veren yeni siteler

  13. Iona表示:

    Trucking Lawyers Near Me Tools To Improve Your Everyday Lifethe Only Trucking Lawyers Near Me Trick That Every Person Should Learn trucking lawyers near me (Iona)

  14. Hey would you mind sharing which blog platform you’re working with?
    I’m planning to start my own blog in the near future but I’m having a tough time
    deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and
    I’m looking for something unique.
    P.S Sorry for getting off-topic but I had to ask!

  15. Generally I do not read article on blogs, but I
    would like to say that this write-up very pressured me to try and do so!

    Your writing taste has been surprised me. Thanks, very nice article.

  16. Derekfar表示:

    matadorbet bid: matadorbet – matadorbet giris

  17. This design is spectacular! You obviously know how to keep a
    reader amused. Between your wit and your videos, I was almost moved to start my
    own blog (well, almost…HaHa!) Great job. I really
    loved what you had to say, and more than that, how you presented it.
    Too cool!

  18. Searching For Inspiration? Look Up Train Accident Lawyer big truck Accident Attorney (idea.informer.com)

  19. 5 Killer Quora Answers On Asbestos Attorney Mesothelioma Mesothelioma lawsuits

  20. Toto Sport Online The Process Isn’t As Hard As You Think 메이저사이트; https://Sportstotosite77662.Gigswiki.Com/,

  21. Watch Out: How Bio-Fire Is Gaining Ground, And What To Do Fireplace Ethanol Insert

  22. Derekfar表示:

    matadorbet: matadorbet bid – matadorbet bid

  23. Diplomi_wfot表示:

    где можно купить диплом о высшем образовании где можно купить диплом о высшем образовании .

  24. Magnificent site. A lot of helpful info here. I’m sending it to some pals ans also sharing in delicious.
    And obviously, thanks for your effort!

  25. ckp.bedzin.pl表示:

    Wow, fantastic blog structure! How lengthy have you been blogging for?

    you make running a blog look easy. The whole look of your website is
    fantastic, as smartly as the content material!

  26. Shay表示:

    Accident Injury Lawyers Near Me Tools To Streamline Your Daily Lifethe
    One Accident Injury Lawyers Near Me Trick Every Person Should
    Know accident injury lawyers near me (Shay)

發佈留言

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