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.
  • 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
    #21

    Wow :) nice one! Thanks!

    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