C# | Check whether an element is contained in the ArrayList - GeeksforGeeks (2024)

Last Updated : 20 Jun, 2022

Summarize

Comments

Improve

Suggest changes

Like Article

Like

Save

Report

ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Contains(Object) method determines whether the element exists in ArrayList or not. Properties of ArrayList Class:

  • Elements can be added or removed from the Array List collection at any point in time.
  • The ArrayList is not guaranteed to be sorted.
  • The capacity of an ArrayList is the number of elements the ArrayList can hold.
  • Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
  • It also allows duplicate elements.
  • Using multidimensional arrays as elements in an ArrayList collection is not supported.

Syntax:

public virtual bool Contains (object item);

Here, item is an Object to locate in the ArrayList. The value can be null. Return Value: This method will return True if the item found in the ArrayList otherwise it returns False. Note: This method performs a linear search, therefore, this method is an O(n) operation, where n is Count. Below are the programs to illustrate the ArrayList.Contains(Object) method: Example 1:

CSHARP

// C# code to check if an element is

// contained in ArrayList or not

using System;

using System.Collections;

using System.Collections.Generic;

class GFG {

// Driver code

public static void Main()

{

// Creating an ArrayList

ArrayList myList = new ArrayList();

// Adding elements to ArrayList

myList.Add("A");

myList.Add("B");

myList.Add("C");

myList.Add("D");

myList.Add("E");

myList.Add("F");

myList.Add("G");

myList.Add("H");

// To check if the ArrayList Contains element "E"

// If yes, then display it's index, else

// display the message

if (myList.Contains("E"))

Console.WriteLine("Yes, exists at index " + myList.IndexOf("E"));

else

Console.WriteLine("No, doesn't exists");

}

}

Output:

Yes, exists at index 4

Example 2 :

CSHARP

// C# code to check if an element is

// contained in ArrayList or not

using System;

using System.Collections;

using System.Collections.Generic;

class GFG {

// Driver code

public static void Main()

{

// Creating an ArrayList

ArrayList myList = new ArrayList();

// Adding elements to ArrayList

myList.Add("5");

myList.Add("7");

myList.Add("9");

myList.Add("11");

myList.Add("12");

myList.Add("16");

myList.Add("20");

myList.Add("25");

// To check if the ArrayList Contains element "24"

// If yes, then display it's index, else

// display the message

if (myList.Contains("24"))

Console.WriteLine("Yes, exists at index " + myList.IndexOf("24"));

else

Console.WriteLine("No, doesn't exists");

}

}

Output:

No, doesn't exists

Time complexity: O(n) since using Contains method

Auxiliary Space: O(n) where n is the size of the ArrayList

Reference:



Please Login to comment...

C# | Check whether an element is contained in the ArrayList - GeeksforGeeks (2024)
Top Articles
How to Use Volume Weighted Average Price (VWAP) in Swing Trading?
FedEx | System Down
417-990-0201
What spices do Germans cook with?
Tyson Employee Paperless
Mr Tire Prince Frederick Md 20678
THE 10 BEST Women's Retreats in Germany for September 2024
Northern Whooping Crane Festival highlights conservation and collaboration in Fort Smith, N.W.T. | CBC News
Teamexpress Login
Top Golf 3000 Clubs
Elle Daily Horoscope Virgo
Chicken Coop Havelock Nc
Inevitable Claymore Wow
Restaurants Near Paramount Theater Cedar Rapids
Jellyfin Ps5
NBA 2k23 MyTEAM guide: Every Trophy Case Agenda for all 30 teams
Babbychula
Dcf Training Number
Aes Salt Lake City Showdown
A Person That Creates Movie Basis Figgerits
8000 Cranberry Springs Drive Suite 2M600
Piedmont Healthstream Sign In
Arrest Gif
Intel K vs KF vs F CPUs: What's the Difference?
Fuse Box Diagram Honda Accord (2013-2017)
Alternatieven - Acteamo - WebCatalog
Craigs List Jax Fl
Iban's staff
Bridger Park Community Garden
Craigs List Stockton
Space Marine 2 Error Code 4: Connection Lost [Solved]
ATM Near Me | Find The Nearest ATM Location | ATM Locator NL
Collier Urgent Care Park Shore
Frcp 47
2020 Can-Am DS 90 X Vs 2020 Honda TRX90X: By the Numbers
2 Pm Cdt
Lonely Wife Dating Club בקורות וחוות דעת משתמשים 2021
Does Target Have Slime Lickers
Academic Notice and Subject to Dismissal
Watch Chainsaw Man English Sub/Dub online Free on HiAnime.to
Nu Carnival Scenes
Colin Donnell Lpsg
Www.homedepot .Com
antelope valley for sale "lancaster ca" - craigslist
Mikayla Campinos Alive Or Dead
Julies Freebies Instant Win
Uno Grade Scale
Jigidi Jigsaw Puzzles Free
What Is The Gcf Of 44J5K4 And 121J2K6
Glowforge Forum
Bunbrat
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 6056

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.