Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Finding number of occurences in a list of numbers

Finding number of occurences in a list of numbers

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay03
    wrote on last edited by
    #1

    hey guys, I was wondering what the most efficient way of finding the number of occurences in a list is. Say I have a list.... struct data{ int number; int occurences; }; data list[7]; list[0].number = 5; list[1].number = 6; list[2].number = 6; list[3].number = 8; list[4].number = 9; list[5].number = 9; list[6].number = 9; how would i sort in such a way that I eliminate the numbers that occur more than once like this, list[0].number = 5; list[1].number = 6; list[2].number = 8; list[3].number = 9; and saves the number of occurence for each number list[0].occurences = 1; list[1].occurences = 2; list[2].occurences = 1; list[3].occurences = 3; I thought of every possible way, I can do it for numbers that appear twice but three times, i duno how to deal with it..... Someone please help me out :doh::doh::doh::doh::doh::doh::confused::confused::confused:

    X M A 3 Replies Last reply
    0
    • J Jay03

      hey guys, I was wondering what the most efficient way of finding the number of occurences in a list is. Say I have a list.... struct data{ int number; int occurences; }; data list[7]; list[0].number = 5; list[1].number = 6; list[2].number = 6; list[3].number = 8; list[4].number = 9; list[5].number = 9; list[6].number = 9; how would i sort in such a way that I eliminate the numbers that occur more than once like this, list[0].number = 5; list[1].number = 6; list[2].number = 8; list[3].number = 9; and saves the number of occurence for each number list[0].occurences = 1; list[1].occurences = 2; list[2].occurences = 1; list[3].occurences = 3; I thought of every possible way, I can do it for numbers that appear twice but three times, i duno how to deal with it..... Someone please help me out :doh::doh::doh::doh::doh::doh::confused::confused::confused:

      X Offline
      X Offline
      XtremDev
      wrote on last edited by
      #2

      Why did you use the name 'list' for a variable which is an array ? Could you post your current algorithm to correct it ?

      A 1 Reply Last reply
      0
      • J Jay03

        hey guys, I was wondering what the most efficient way of finding the number of occurences in a list is. Say I have a list.... struct data{ int number; int occurences; }; data list[7]; list[0].number = 5; list[1].number = 6; list[2].number = 6; list[3].number = 8; list[4].number = 9; list[5].number = 9; list[6].number = 9; how would i sort in such a way that I eliminate the numbers that occur more than once like this, list[0].number = 5; list[1].number = 6; list[2].number = 8; list[3].number = 9; and saves the number of occurence for each number list[0].occurences = 1; list[1].occurences = 2; list[2].occurences = 1; list[3].occurences = 3; I thought of every possible way, I can do it for numbers that appear twice but three times, i duno how to deal with it..... Someone please help me out :doh::doh::doh::doh::doh::doh::confused::confused::confused:

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        If you can use an STL container, do so and then you can use the sort and unique methods.

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

        1 Reply Last reply
        0
        • J Jay03

          hey guys, I was wondering what the most efficient way of finding the number of occurences in a list is. Say I have a list.... struct data{ int number; int occurences; }; data list[7]; list[0].number = 5; list[1].number = 6; list[2].number = 6; list[3].number = 8; list[4].number = 9; list[5].number = 9; list[6].number = 9; how would i sort in such a way that I eliminate the numbers that occur more than once like this, list[0].number = 5; list[1].number = 6; list[2].number = 8; list[3].number = 9; and saves the number of occurence for each number list[0].occurences = 1; list[1].occurences = 2; list[2].occurences = 1; list[3].occurences = 3; I thought of every possible way, I can do it for numbers that appear twice but three times, i duno how to deal with it..... Someone please help me out :doh::doh::doh::doh::doh::doh::confused::confused::confused:

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          Make a new array for the initial list of numbers then use a for loop to start adding the numbers to the array, use an embedded for loop to check whether the number has already been added. for(int i=0; l <7; i++) { list[i].number = 0; list[i].occurences = 0; } int ListIndex = 0; for(i=0; i <7; i++) { int index = i; bool IsRepeat = false; for(int j=0; j <7; j++) { if(array[index] == list[j].number) { IsRepeat = true; list[j].occurences++; } } if(!IsRepeat) { list[ListIndex].number = array[i]; list[ListIndex].occurences = 1; ListIndex++; } } I just stuck that togeather now, if it don't work, then don't complain about it ;P -- modified at 6:27 Friday 10th November, 2006 The code does work, im so happy :)

          J 1 Reply Last reply
          0
          • X XtremDev

            Why did you use the name 'list' for a variable which is an array ? Could you post your current algorithm to correct it ?

            A Offline
            A Offline
            Anthony Mushrow
            wrote on last edited by
            #5

            Am i missing soemthing? Why not call an array a list, thats basically what it is.

            J 1 Reply Last reply
            0
            • A Anthony Mushrow

              Am i missing soemthing? Why not call an array a list, thats basically what it is.

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #6

              The Undefeated wrote:

              Why not call an array a list, thats basically what it is.

              I'd like to be shown where 'Array' publically derives from ("is-a") 'List'. ;) Also, in C++, 'Array' has the distinct connotation of 'contiguous memory'. A 'List' has definitly not. If he would have called his std:vector 'Array'... While on a very zoomed-out, global point of wiew you are right, for a beginner it is definitely better to separate the concepts of the two container types.


              "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

              1 Reply Last reply
              0
              • A Anthony Mushrow

                Make a new array for the initial list of numbers then use a for loop to start adding the numbers to the array, use an embedded for loop to check whether the number has already been added. for(int i=0; l <7; i++) { list[i].number = 0; list[i].occurences = 0; } int ListIndex = 0; for(i=0; i <7; i++) { int index = i; bool IsRepeat = false; for(int j=0; j <7; j++) { if(array[index] == list[j].number) { IsRepeat = true; list[j].occurences++; } } if(!IsRepeat) { list[ListIndex].number = array[i]; list[ListIndex].occurences = 1; ListIndex++; } } I just stuck that togeather now, if it don't work, then don't complain about it ;P -- modified at 6:27 Friday 10th November, 2006 The code does work, im so happy :)

                J Offline
                J Offline
                Jay03
                wrote on last edited by
                #7

                thanks, thats great i got a bit confused when I first saw your code, but....... it works!!! THANKS !!!!!!! :-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:rose::rose:

                A 1 Reply Last reply
                0
                • J Jay03

                  thanks, thats great i got a bit confused when I first saw your code, but....... it works!!! THANKS !!!!!!! :-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:rose::rose:

                  A Offline
                  A Offline
                  Anthony Mushrow
                  wrote on last edited by
                  #8

                  Just so long as you make sure you understand it, all will be good ;)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups