I have a list and an array. I want to find out the number/count of elements in the array that match those in the list
List<int> list = new List<int>{ 1, 2, 3, 4 };
int[] array = new int[] { 1, 2 };
Since the two matching elements are 1 and 2, I am expecting a result of count 2.
Can someone please point me in the right direction?
You can use a little Linq with the Count extension method:
var count = array.Count(list.Contains);
Or if you know that there are no duplicate values in the the array, you can use the Intersect method:
var count = array.Intersect(list).Count();
You can use:
int matches = list.Intersect(array).Count();
Note that this will only work if the list and array only contain unique values.
Related
I'm looking to split an ordered list into multiple lists based off of the index which gets supplied from a another list.
Starting list 1,2,3,4,5,6,7,8,9,10
Index list 0,3,8
Expected lists of lists
1,2,3
4,5,6,7
8,9,10
Index List will always start with an index of 0. The expected list will contains all items from the starting list until Index List + 1, and so forth.
An index list of 0,2,8 would result in 1,2 3,4,5,6,7,8 9,10
I've tried using GetRange and keeping track of where the next index started, then using the difference to add to a list however the amount of items being added to the expected list isn't correct.
var list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var indexList = new List<int> { 0, 2, 8 };
// using zip for pairwise selection we can calculate lengths of each segment
var listOfLists = indexList.Zip(indexList.Skip(1).Append(list.Count))
.Select(i => list.GetRange(i.First, i.Second - i.First))
.ToList();
I'm hoping this will be fairly simple and down to my lack of knowledge as a beginner but I'm trying to see if an array of ints with two elements is in a List.
Int[] meh = {1,2};
List<int[]> list1 = new List<int[]>();
List1.Add(meh);
Int[] meh2 = {1,2};
If(List1.Contains(meh2))
{
Console.WriteLine(“Found it”);
}
From reading around I gather that the array wont be found as it's to do with how Lists compare objects by reference and not value ... all examples Ive found have been to find a single int within an array in a List but not the array as a whole.
Im vaguely aware that List.Find() may be useful here but again I cant see how to use LINQ for matching both elements in each array in the list.
Any help or pointers to reading material greatly appreciated.
Thanks in advance.
How about this
if(list1.Any(x => x.SequenceEqual(meh2)))
{
Console.WriteLine("Found it");
}
You can use Enumerable.SequenceEqual
that will return -1 if the sequence is not found
example:
var myArr = new int[] { 3, 3 };
List<int[]> ListOfArrays = new List<int[]>
{
new int[] { 0, 0 },
new int[] { 2, 2 },
new int[] { 1, 1 },
new int[] { 3, 3 }
};
var index = ListOfArrays.FindIndex(l => Enumerable.SequenceEqual(myArr, l));
Console.WriteLine("here: " + index);
Not sure what you want to achieve exactly. If list1 has an element [1, 2, 3] and you search for [1, 2], would that be a match, or do all elements have to match? Do they have to be in the same order? ...
Whatever you want to achieve exactly, you can do it with Any() and SequenceEqual()
int[] meh = {1, 2};
int[] meh2 = {1, 5};
var list1 = new List<int[]>() {meh, meh2};
if (list1.Any(x => x.SequenceEqual(new[] {1, 5})))
{
Console.WriteLine("Found it");
}
Also see this answer. It contains an example where both arrays are sorted first (ie ignoring the order of the elements in the array)
I have the below array:
int [] array = { 9, 8, 3, 2, 3, 2 };
I'd like to write a statement when i pick a number from array, it gives the following number as the result.
for examaple i pick the number 8 and according to the statement it gives to number 3 as result.
One way to do it is to use SkipWhile to reach the location of the search number, skip one, and take the first item after it:
var array = new[] { 9, 8, 3, 2, 3, 2 };
var next = array.SkipWhile(n => n != 8).Skip(1).First(); // next==3
This code assumes two things:
The search number 8 is there, and
The search number is not the last number in the sequence.
Demo.
If I understand your question correctly you want to return the next item in the array after the selection?
If that is the case you can do the following:
int index = Array.IndexOf(array, 8);
return array[index + 1];
There are some limitations to this implementation, please see here:
https://msdn.microsoft.com/en-us/library/7eddebat(v=vs.110).aspx
How can we delete all array elements between 2 numbers?
For example : The array is {2,6,3,6,8,2,7,2}
The user writes in two numbers, let's say 2 and 4.
That causes the program to delete every array elements between the 2nd and 4th position.
In this case, it deletes : 3,6,8
You cannot delete items from an array. But you can create a new array that contains only the items that you want to keep. In your case you can use the following:
int[] array = {2, 6, 3, 6, 8, 2, 7, 2};
array = array.Where((_, i) => i < 2 || i > 4).ToArray();
By the way, if you use a List instead of an array, then you can remove items. Consider the following example:
List<int> list = new List<int>() {2, 6, 3, 6, 8, 2, 7, 2};
for(int i = 4; i >= 2 ; i--)
{
list.RemoveAt(i);
}
For lists, you can use RemoveRange to do exactly that. It’s just that instead of the (inclusive) end index, you need to pass the number of elements you want to delete. So for inclusive indexes start and end it would look like this:
list.RemoveRange(start, end - start + 1);
For arrays, you cannot really do this as once created arrays have a fixed size. If you really need an array, you could create a list from the array, remove the items, and then create an array again using ToArray.
As Yacoub Massad mentioned in his answer: You cannot delete items from an array. But you can create a new array that contains only the items that you want to keep. In this case I would use linq, it's super easy:
int[] array = {2, 6, 3, 6, 8, 2, 7, 2};
int x = 2;
int y = 4;
var array1 = array.ToList()
.Take(x).Concat(array.ToList().Skip(x+y-1))
.ToArray();
foreach(var i in array1)
{
Console.Write(i);
Console.Write(',');
}
Result:
2,6,2,7,2,
DotNetFiddle Example
I have an Arraylist In Which I want to remove the single top element from Arraylist(i.e. POP in Stack),I have used .Remove() method, But it doesn't work,So How can i remove only single element from arraylist
For example-If arraytlist contains 96,97,98,99,100 When used .RemoveAt(0) Its going to remove the element 96,I want to remove the item 100 From arraylist ,So how can i remove this top item?
Try using RemoveAt:
ArrayList list = …
// remove the last item
list.RemoveAt(list.Count - 1);
You may try to use RemoveAt:
ArrayList ar = new ArrayList();
ar.Add("Delete");
ar.Add("The");
ar.Add("Top element");
ArrayList.RemoveAt(0);
Removes the element at the specified index of the ArrayList.
Also to quote the remarks:
In collections of contiguous elements, such as lists, the elements
that follow the removed element move up to occupy the vacated spot. If
the collection is indexed, the indexes of the elements that are moved
are also updated. This behavior does not apply to collections where
elements are conceptually grouped into buckets, such as a hash table.
int[] numbers = { 1, 3, 4, 9, 2, 4 };
int numToRemove = 1;
int numIdx = Array.IndexOf(numbers, numToRemove);
List<int> tmp = new List<int>(numbers);
tmp.RemoveAt(numIdx);
numbers = tmp.ToArray();
ArrayList myAL = new ArrayList();
....
myAL.RemoveAt(myAL.Count - 1);
Try this
First you find the array length using below code
int length = yourarray.Length;
next put this length in removeat
yourarray.RemoveAt(length - 1);