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 1 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 Offline
    W Offline
    Wheels012
    wrote on last edited by
    #1

    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

    L L I J P 5 Replies 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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Wheels012 wrote:

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

      This will always be false.

      L W 2 Replies 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

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

        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.


        W P 2 Replies 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

          I Offline
          I Offline
          Islorvat
          wrote on last edited by
          #4

          You can try numbers.Contains(intRnd). Hope this helps. Good luck!

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


            W Offline
            W Offline
            Wheels012
            wrote on last edited by
            #5

            I am pretty new to C# and don't know what a HashSet is, but I will look it up. Originally I had the array declaration in the method and set as the numbe rof items on a list, but I thought that would clear the array everytime the method was called. I believe the find is working well, but I need to load the array with a number (index of list) if it is not currently in there. WHEELS

            L 1 Reply Last reply
            0
            • I Islorvat

              You can try numbers.Contains(intRnd). Hope this helps. Good luck!

              W Offline
              W Offline
              Wheels012
              wrote on last edited by
              #6

              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 realJSOPR 2 Replies Last reply
              0
              • L Lost User

                Wheels012 wrote:

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

                This will always be false.

                W Offline
                W Offline
                Wheels012
                wrote on last edited by
                #7

                How do you check for empty spots (elements?) in a one dimensioal array? WHEELS

                L 1 Reply Last reply
                0
                • L Lost User

                  Wheels012 wrote:

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

                  This will always be false.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Go ahead and univote it. That won't change the truth.

                  L 1 Reply Last reply
                  0
                  • W Wheels012

                    I am pretty new to C# and don't know what a HashSet is, but I will look it up. Originally I had the array declaration in the method and set as the numbe rof items on a list, but I thought that would clear the array everytime the method was called. I believe the find is working well, but I need to load the array with a number (index of list) if it is not currently in there. WHEELS

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

                    the problem with arrays of integers is, you don't have any "empty" positions, the array initially is filled with zeroes, which could also be valid inputs. the solution is to use some "elastic" collection, that grows when things get added (and even shrinks when things get removed again).

                    Wheels012 wrote:

                    Originally I had the array declaration in the method ... that would clear the array everytime

                    correct, whatever you use it should be initialized only once.

                    Wheels012 wrote:

                    I will look it up

                    good. you may learn your code could shrink dramatically... :)

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


                    Merry Christmas and a Happy New Year to all.


                    1 Reply Last reply
                    0
                    • W Wheels012

                      How do you check for empty spots (elements?) in a one dimensioal array? WHEELS

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      You can't, an array never has empty spots, it just has entries with the default value, which is 0 (zero) for int. But you can't see the difference between a zero that is there because you never put anything in that place and a zero that you put there.

                      1 Reply Last reply
                      0
                      • L Lost User

                        Go ahead and univote it. That won't change the truth.

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

                        Some people lack the wisdom to recognize the help they are getting... :thumbsup:

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


                        Merry Christmas and a Happy New Year to all.


                        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

                          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
                                        • 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
                                          #20

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