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. value duplication issue

value duplication issue

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsdata-structureshelp
5 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.
  • P Offline
    P Offline
    Paul Swet
    wrote on last edited by
    #1

    The issue is: You are given an array of integers of size n containing values in the range 1 to n-1. Obviously there’s at least one duplicate value in this array. Please let me know the algorithm for finding one such duplicate value. Thanks.

    D A C 3 Replies Last reply
    0
    • P Paul Swet

      The issue is: You are given an array of integers of size n containing values in the range 1 to n-1. Obviously there’s at least one duplicate value in this array. Please let me know the algorithm for finding one such duplicate value. Thanks.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Hint: sort the array.


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      1 Reply Last reply
      0
      • P Paul Swet

        The issue is: You are given an array of integers of size n containing values in the range 1 to n-1. Obviously there’s at least one duplicate value in this array. Please let me know the algorithm for finding one such duplicate value. Thanks.

        A Offline
        A Offline
        Arsalan Malik
        wrote on last edited by
        #3

        Why is it obvious that there's atleast one duplicate value in this array?:) ARSALAN MALIK

        P 1 Reply Last reply
        0
        • A Arsalan Malik

          Why is it obvious that there's atleast one duplicate value in this array?:) ARSALAN MALIK

          P Offline
          P Offline
          Paul Swet
          wrote on last edited by
          #4

          Since that the array size is n and the values are from 1 to n-1

          1 Reply Last reply
          0
          • P Paul Swet

            The issue is: You are given an array of integers of size n containing values in the range 1 to n-1. Obviously there’s at least one duplicate value in this array. Please let me know the algorithm for finding one such duplicate value. Thanks.

            C Offline
            C Offline
            cmk
            wrote on last edited by
            #5

            If the array is sorted then use a binary search to check the value at each position against the value of the position : if ( a[i] == i+1 ) then the duplicate is in the section greater than i else the duplicate is in the section less than or equal to i. If the array isn't sorted then allocate a temp array of size n and walk through the original putting 'check marks' in the temp array at the appropriate index (value-1). As soon as you find a value that has alreay been 'checked' you have your duplicate. ...cmk Save the whales - collect the whole set

            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