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. array issue

array issue

Scheduled Pinned Locked Moved C#
algorithmsdata-structureshelp
2 Posts 2 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.

    T 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.

      T Offline
      T Offline
      tdciDoug
      wrote on last edited by
      #2

      There are many ways you can go about doing this. There are a few questions you should look at first though. Is the array sorted? Can you define another data structure? Are you trying to keep the complexity below a certain level? I might do something like the following: for (int iSize = 0; iSize < yourArray.Count; iSize++) { while (yourArray[iSize] != iSize) { if (yourArray[iSize] == yourArray[yourArray[iSize]]) { MessageBox.Show( "Duplicate value of " + yourArray[iSize].ToString() ); iSize = yourArray.Count; break; } Swap(yourArray[iSize], yourArray[yourArray[iSize]]); } } I didn't really test this, just pulled it off the top of my head real quick. It might work, but I can't give any guarantees. Good luck

      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