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#
  4. Without O(n2)

Without O(n2)

Scheduled Pinned Locked Moved C#
data-structuresquestion
4 Posts 4 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.
  • H Offline
    H Offline
    h s n
    wrote on last edited by
    #1

    I want to find first duplicate occurence in an array. Can I do it without O(n2) ???

    X L D 3 Replies Last reply
    0
    • H h s n

      I want to find first duplicate occurence in an array. Can I do it without O(n2) ???

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

      go through the array and use a dictonary where object is the array element (or some unique identifer for the items in your array) and int is the count of occurances. go through the array once and add items into the dictonary if it doesn't exist, or increment the count value (the dictonary is a hash so it will be really fast in finding the key/seeing if it exists). after going through the array once, iterate each KeyValuePair in the dictionary and look for any int values greater than 1. your complexity is about O(2n + 1).

      1 Reply Last reply
      0
      • H h s n

        I want to find first duplicate occurence in an array. Can I do it without O(n2) ???

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

        Hi, in pseudo-C#

        List myList=empty;
        foreach (element elem in inputcollection) {
        if myList contains elem throw new FoundDuplicateException(elem)
        else add elem to myList
        }
        // no duplicates

        :)

        Luc Pattyn


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


        1 Reply Last reply
        0
        • H h s n

          I want to find first duplicate occurence in an array. Can I do it without O(n2) ???

          D Offline
          D Offline
          dino2094
          wrote on last edited by
          #4

          Is this a homework question? 1. Sort the array but keep track of the original positions. 2. Set FOO to size. 3. Compare n with n+1 to find duplicates. If the original positions of the duplicates are less than FOO, set FOO the larger of the original locations. This assumes a number can be repeated once, but it is not hard to modify for multiple occurrences. 4. If FOO == size, there are no duplicates else FOO is the location of the first duplicate. Step 1 is O(nlogn) Step 3 is O(n)

          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