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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Algorithms
  4. finding all recurring elements in an array

finding all recurring elements in an array

Scheduled Pinned Locked Moved Algorithms
algorithmsdata-structuresquestion
7 Posts 5 Posters 27 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.
  • R Offline
    R Offline
    Rocky
    wrote on last edited by
    #1

    Hello Can any one plz give me an algorithm to find all the elements in an array that are repeated atleast once? Thanks in advance Rocky

    E P 2 Replies Last reply
    0
    • R Rocky

      Hello Can any one plz give me an algorithm to find all the elements in an array that are repeated atleast once? Thanks in advance Rocky

      E Offline
      E Offline
      El Corazon
      wrote on last edited by
      #2

      Rocky71 wrote:

      Can any one plz give me an algorithm to find all the elements in an array that are repeated atleast once?

      brute force: read all, check for match on all. Now if you want minimal search, now you are talking fun....

      _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

      D R 2 Replies Last reply
      0
      • E El Corazon

        Rocky71 wrote:

        Can any one plz give me an algorithm to find all the elements in an array that are repeated atleast once?

        brute force: read all, check for match on all. Now if you want minimal search, now you are talking fun....

        _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

        D Offline
        D Offline
        Dan Neely
        wrote on last edited by
        #3

        Just add a sort pass. With potential duplicates adjacent to each other an O(n) detection routine is trivial.

        -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

        E 1 Reply Last reply
        0
        • D Dan Neely

          Just add a sort pass. With potential duplicates adjacent to each other an O(n) detection routine is trivial.

          -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

          E Offline
          E Offline
          El Corazon
          wrote on last edited by
          #4

          dan neely wrote:

          Just add a sort pass.

          true, it said nothing about order. There you go, two algorithms, issue solved. :)

          _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

          1 Reply Last reply
          0
          • E El Corazon

            Rocky71 wrote:

            Can any one plz give me an algorithm to find all the elements in an array that are repeated atleast once?

            brute force: read all, check for match on all. Now if you want minimal search, now you are talking fun....

            _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

            R Offline
            R Offline
            Rocky
            wrote on last edited by
            #5

            yea i guess there's no other option but to use brute force

            L 1 Reply Last reply
            0
            • R Rocky

              yea i guess there's no other option but to use brute force

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              well, what did you expect ? Here are two approaches without any visible sorting: 1. just enter the numbers one by one in a hashtable; use the numbers as the key, and their multiplicity as the value. So to add a[i], check for presence of key a[i]; if not, add the pair (a[i], 1), else replace (a[i], value) by (a[i], value+1). When done, enumerate the keys with value>=2 2. Since you are not interested in actual multiplicity values, you could try two simple lists: one for "seen just once", the other for "seen more than once". Now for each a[i] first test the "more than once", if not present, the "just once", and move/add the item appropriately. Dont be surprised if this takes longer, for new numbers you are searching two lists (albeit they both are smaller). For both of the above methods, here is a big optimization: just do it for all numbers but the last one. If these all appear more than once, there is no need to read the last number at all. :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              1 Reply Last reply
              0
              • R Rocky

                Hello Can any one plz give me an algorithm to find all the elements in an array that are repeated atleast once? Thanks in advance Rocky

                P Offline
                P Offline
                PICguy
                wrote on last edited by
                #7

                If you want the elements themselves simply sort your list. If you also need the original index values you will have to sort element numbers along with the elements. After sorting the problem becomes trivial.

                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