Hi
I want to het the position of the two "Ford" in the array. The first is not a problem, but how to get the next?
Thanks
string[] cars = { "Volvo", "BMW", "Ford", "Mazda","Ford" };
int p = Array.IndexOf(cars, "Ford");
Console.WriteLine("Ford is at position {0} ", p + 1);
//p = Array.IndexOf(cars, "Ford", 1);
//Console.WriteLine("Ford is on position {0} ", p + 1);
poojasree raviPosted Dec 8, 2021, 12:30 PM
to search position of two identical elements in an array try to print the index position of the elements using a for loop
static void Main(string[] args)
{
string[] cars = {"Volvo", "BMW", "Ford", "Mazda","Ford" };
var result= cars.Distinct().ToArray();
foreach (var item1 in result)
{
int count = 0;
int index = 0;
foreach (var item in cars)
{
if (item1 == item)
{
count += 1;
if (count > 1)
Console.WritrLine($"Index pos of repeated element {item1} at index " + index);
}
index += 1;
}
}
}
Refer: https://www.dotnetperls.com/array-indexof
Valerie MeunierPosted Dec 8, 2021, 2:52 PM