ANEES
ANEES

Reputation: 300

How to check single array contains same value

string currentUrl = "destination/India/India/mumbai";
string[] array = currentUrl.Split('/');
string countryName = "india";

In the above example,In C# how can i check that Country name is repeating in url?

Upvotes: 2

Views: 84

Answers (2)

Mehul Bhalala
Mehul Bhalala

Reputation: 800

you can use the Distinct method. Something like

string currentUrl = "destination/India/mumbai";
string[] array = currentUrl.Split('/');
string countryName = "india";

if (array.Distinct().Count() != array.Count())
{
    Console.WriteLine("Duplicate");
}

Or you can try blow code

string currentUrl = "destination/india/india/mumbai";
string[] array = currentUrl.Split('/');
string countryName = "india";

bool r = array.Where(x => x.Equals(countryName)).Count()>1;
if(r)
{
    Console.WriteLine("Duplicate");
}

Upvotes: 1

fubo
fubo

Reputation: 45947

By Count()ing the appearence of countryName

string currentUrl = "destination/India/India/mumbai";
string[] array = currentUrl.Split('/');
string countryName = "india";
bool isRepeating = array.Count(x => countryName.Equals(x, StringComparison.OrdinalIgnoreCase)) > 1;

Upvotes: 5

Related Questions