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. C#
  4. C# test if number exists in array and if not put it in

C# test if number exists in array and if not put it in

Scheduled Pinned Locked Moved C#
csharpdatabasedata-structures
22 Posts 9 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.
  • W Wheels012

    I believe my find is working alright, but I have to figure out how to load a number in the array if it is not in there. WHEELS

    A Offline
    A Offline
    Alex Manolescu
    wrote on last edited by
    #12

    I have an idea of how you can solve you're problem, below are the steps: Objective: What do we know? We know that you are looking for 2 identical numbers always. If there is only 1 number, then add his duplicate. Solution: Supose the number we are looking for his duplicate is X, the first number of the array. 1. sort the array 2. ask for the neighbour of the number X, at right! if duplicate is at right increment index to the next X number (skip the duplicate we found!) else append the number to the array and repeat step 1 3. repeat until the end of the array. This is the pseudocode of the solution I've taught. Hope this helps. :) Cheer's, Alex Manolescu.

    1 Reply Last reply
    0
    • W Wheels012

      I believe my find is working alright, but I have to figure out how to load a number in the array if it is not in there. WHEELS

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #13

      You cannot add to an array. You're going to have to use a list. If you don't know the difference between an array and a list, use google.

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      A 1 Reply Last reply
      0
      • realJSOPR realJSOP

        You cannot add to an array. You're going to have to use a list. If you don't know the difference between an array and a list, use google.

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        A Offline
        A Offline
        Alex Manolescu
        wrote on last edited by
        #14

        John Simmons / outlaw programmer is right! If you know the number of elements in you're array, you can create an array of dimension = elements x 2 (for the duplicates). If you don't know the number of elements then you need to use another data structure, for example: a linked list! :) Cheer's, Alex Manolescu.

        realJSOPR 1 Reply Last reply
        0
        • W Wheels012

          Good afternoon. I was wondering if there was a better was to check for duplicate numbers in an array and if it is not in the array, put it in. This is what I have so far:

          int[] numbers = new int[1000]; //Global

          private bool RndDuplicate(int intRnd)
          {
          int index = Array.BinarySearch(numbers, 0, numbers.Length, intRnd);

                  if (index > 0)
                  {
                      return true;
                  }
                  else
                  {
                      //Load array
                      for (int i = 0; i < numbers.Length; i++)
                      {
                          if (numbers\[i\].ToString() == "")
                          {
                              numbers\[i\] = intRnd;
                              break;
                          }
                      }
                      return false;
                  }
              }      
          

          Thank you, WHEELS

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #15

          Wheels012 wrote:

          Array.BinarySearch(numbers, 0, numbers.Length, intRnd);

          It looks like you're just throwing random numbers (trying to, anyway) onto the end of the array, so how is this supposed to work if numbers is unsorted? Like others have said, use a List and when adding to it insert items at their proper index to keep it sorted, or use a hash table. :badger:

          1 Reply Last reply
          0
          • L Luc Pattyn

            Wheels012 wrote:

            int[] numbers = new int[1000];

            where is this magic 1000 coming from? that is bad code.

            Wheels012 wrote:

            numbers[i].ToString() == ""

            and which number will ever have a ToString() result of ""? the normal approach would be based on a HashSet, not an array. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Merry Christmas and a Happy New Year to all.


            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #16

            Luc Pattyn wrote:

            HashSet

            That's what I was going to suggest as well.

            1 Reply Last reply
            0
            • W Wheels012

              Good afternoon. I was wondering if there was a better was to check for duplicate numbers in an array and if it is not in the array, put it in. This is what I have so far:

              int[] numbers = new int[1000]; //Global

              private bool RndDuplicate(int intRnd)
              {
              int index = Array.BinarySearch(numbers, 0, numbers.Length, intRnd);

                      if (index > 0)
                      {
                          return true;
                      }
                      else
                      {
                          //Load array
                          for (int i = 0; i < numbers.Length; i++)
                          {
                              if (numbers\[i\].ToString() == "")
                              {
                                  numbers\[i\] = intRnd;
                                  break;
                              }
                          }
                          return false;
                      }
                  }      
              

              Thank you, WHEELS

              P Offline
              P Offline
              petercrab
              wrote on last edited by
              #17

              I would definitely use a List(dynamic array) for what you want.

                  List<int> numbers = new List<int>();
              
                  private bool RndDuplicate(int intRnd)
                  {
                      if (numbers.Contains(intRnd))
                          return true;
                      else
                      {
                          numbers.Add(intRnd);
                          return false;
                      }
                  }
              
              L W 2 Replies Last reply
              0
              • A Alex Manolescu

                John Simmons / outlaw programmer is right! If you know the number of elements in you're array, you can create an array of dimension = elements x 2 (for the duplicates). If you don't know the number of elements then you need to use another data structure, for example: a linked list! :) Cheer's, Alex Manolescu.

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #18

                Alex Manolescu wrote:

                you can create an array of dimension = elements x 2 (for the duplicates).

                But you would only do that if you're a retard, or completely new at programming.

                Alex Manolescu wrote:

                If you don't know the number of elements then you need to use another data structure, for example: a linked list!

                Just use a List object. It lets you add/remove items, and even sort them. Jeeze!

                .45 ACP - because shooting twice is just silly
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                A 1 Reply Last reply
                0
                • P petercrab

                  I would definitely use a List(dynamic array) for what you want.

                      List<int> numbers = new List<int>();
                  
                      private bool RndDuplicate(int intRnd)
                      {
                          if (numbers.Contains(intRnd))
                              return true;
                          else
                          {
                              numbers.Add(intRnd);
                              return false;
                          }
                      }
                  
                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #19

                  way too much code, and too slow for large sets.

                  HashSet set=new HashSet();

                  private bool RndDuplicate(int intRnd) {return !set.Add(intRnd);}

                  :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Merry Christmas and a Happy New Year to all.


                  A 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    way too much code, and too slow for large sets.

                    HashSet set=new HashSet();

                    private bool RndDuplicate(int intRnd) {return !set.Add(intRnd);}

                    :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    Merry Christmas and a Happy New Year to all.


                    A Offline
                    A Offline
                    Alex Manolescu
                    wrote on last edited by
                    #20

                    Wow :) nice one! Thanks!

                    1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      Alex Manolescu wrote:

                      you can create an array of dimension = elements x 2 (for the duplicates).

                      But you would only do that if you're a retard, or completely new at programming.

                      Alex Manolescu wrote:

                      If you don't know the number of elements then you need to use another data structure, for example: a linked list!

                      Just use a List object. It lets you add/remove items, and even sort them. Jeeze!

                      .45 ACP - because shooting twice is just silly
                      -----
                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                      A Offline
                      A Offline
                      Alex Manolescu
                      wrote on last edited by
                      #21

                      John Simmons / outlaw programmer wrote:

                      Jeeze!

                      I've just replay my opinion. I've taught I could help to understand better the idea. No need to involve Jesus here! Cheer's, Alex Manolescu.

                      1 Reply Last reply
                      0
                      • P petercrab

                        I would definitely use a List(dynamic array) for what you want.

                            List<int> numbers = new List<int>();
                        
                            private bool RndDuplicate(int intRnd)
                            {
                                if (numbers.Contains(intRnd))
                                    return true;
                                else
                                {
                                    numbers.Add(intRnd);
                                    return false;
                                }
                            }
                        
                        W Offline
                        W Offline
                        Wheels012
                        wrote on last edited by
                        #22

                        Thank you petercrab. That is just what I am looking for. Happy New Year, WHEELS

                        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