This question already has answers here:
Is there a built-in method to compare collections?
(15 answers)
Functional way to check if array of numbers is sequential
(7 answers)
Closed 4 years ago.
I am looking for a faster and more accurate way to check a Sequence:
List<int> sequence = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 … 41}
private bool IsSequential(List<int> sequence)
{
int S = sequence[0];
int T = sequence[sequence.Count- 1];
List<int> Possible = Enumerable.Range(S, T).ToList();
List<int> Except = sequence.Except(Possible).ToList();
if (Except.Count == 0)
return true;
else
return false;
}
My code returns 1 if the list is the same, I have some sort of count issue?
I wonder if there is a better way to check an integer sequence: 200, 201, 202... and so on.
Some Sequences may be out of sequence: 200, 210, 203, 204... I need to identify this issue.
Thanks
You can try like following using SequenceEqual.
List<int> sequence = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
bool isInSequence = sequence.SequenceEqual(Enumerable.Range(sequence[0], sequence.Count()));
Related
This question already has answers here:
Finding a subsequence in longer sequence
(8 answers)
Closed 2 years ago.
I have List of Lists or arrays. It contains certain ordered sequences:
0 1 2 3
4 5 6
23 24 25 28
etc.
I want to add another sequence, but only if it's unique (easy) and any of the Lists does not contain it. So for example:
0 1 2
will be rejected, and function will return false, and
9 10
will be accepted, and function will return true.
If I understand you correctly, you want to search through all the lists in a List<List<int>> to see if any contain a set of numbers, and if none do, then add the numbers as a new list.
One way to do this is using Linq:
public static void AddListIfNotExist(List<List<int>> lists, List<int> newList)
{
if (lists == null || newList == null) return;
if (!lists.Any(list => newList.All(item => list.Contains(item))))
{
lists.Add(newList);
}
}
In use it might look like:
var lists = new List<List<int>>
{
new List<int> { 0, 1, 2, 3 },
new List<int> { 4, 5, 6 },
new List<int> { 23, 24, 25, 28 }
};
var newList1 = new List<int> { 0, 1, 2 };
var newList2 = new List<int> { 9, 10 };
AddListIfNotExist(lists, newList1);
AddListIfNotExist(lists, newList2);
This question already has answers here:
Add zero-padding to a string
(6 answers)
C# convert int to string with padding zeros?
(15 answers)
Simple and elegant way to zero-pad a value in C# [duplicate]
(4 answers)
Closed 4 years ago.
So I Have a List, but the list is in integer.
Like 1, 2, 3, 4, 5,...,10,...100,..
And I want to convert that integer to string with format 00000X.
X is represent the integer. So the list will be 000001, 000002, 000010, 000100, etc.
you can achive this by using LINQ. Please check below answer.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 10, 100, 1000,10000,99999,100000 };
var mask = "00000";
List<string> stringNumbers = numbers.Select(x =>
{
if (mask.Length > x.ToString().Length)
{
return mask.Substring(0, mask.Length - x.ToString().Length) + x.ToString();
}
else return x.ToString();
}).ToList();
List<string> result = new List<string>();
foreach (int yourNumber in intList) {
result.Add("0000000".Substring(0, 7 - yourNumber.ToString().Length);
}
Sould work.
You also can do in on one line, intList.ForEach(...
This question already has answers here:
Merging two arrays in .NET
(26 answers)
Closed 6 years ago.
I'm trying to make a console app with two double type arrays, but i have a problem.
How can i add two arrays into one, but without repeating same number (i have same number in two arrays)?
Thank you very much
Use Union from Linq:
double[] a = new double[] { 1, 2, 3, 4 };
double[] b = new double[] { 3, 4, 5, 6 };
double[] arr = a.Union(b).ToArray();
Variable arr will now contain 1, 2, 3, 4, 5, 6.
This question already has answers here:
Fastest way to Remove Duplicate Value from a list<> by lambda
(7 answers)
Closed 6 years ago.
So my code is basically this:
List list = new List(new int[] { 1, 5, 8, 8, 8, 2, 3, 3, 4, });
list = RemoveDuplicats(list);
public static List<int> RemoveDuplicats(List<int> list)
{
int i = 0;
while (i<list.Count)
{
if (list[i] == list[i+1])
{
list.RemoveAt(list[i + 1]);
return list;
}
i++;
}
return list;
}
It seems like RemoveAt is not working or it's being skipped entirely. So what i should be getting is 1,5,8,2,3,4 but it just prints original list. Where'd i go wrong?
Use the Distinct IEnumerable extension
List<int> list = new List<int>() { 1, 5, 8, 8, 8, 2, 3, 3, 4, };
list = list.Distinct().ToList();
Returns distinct elements from a sequence by using the default
equality comparer to compare values.
However, a part from this solution that is explained in many duplicates of your question, I wish to explain your error.
Your current code doesn't work correctly because when you find the first duplicate you call RemoveAt passing the value of the element (8), not the index (2) of the element to be removed as RemoveAt requires. And you are lucky that this doesn't create an Out of Range exception. Moreover you exit immediately with a return leaving the other duplicates in place.
If you still want to use an hand made remove code you could try with this
public List<int> RemoveDuplicats(List<int> list)
{
int i = 0;
List<int> distinctElements = new List<int>();
while (i < list.Count)
{
if (!distinctElements.Contains(list[i]))
distinctElements.Add(list[i]);
i++;
}
return distinctElements;
}
I would use distinct.
list.Distinct().ToList()
This question already has answers here:
Comparing two arrays in C#
(11 answers)
Closed 7 years ago.
i can't find same values in two integer arrays. Here is my code
int[] array1 = { 4, 5, 6, 7, 8, 9, 10 };
int[] array2 = { 1, 2, 3, 4, 5, 6 };
How to check same value?
I tried;
IEnumerable<int> k = (from d1 in array1
select d1).Intersect(from d2 in array2
select d2).ToList();
foreach (var item in k)
{
Console.WriteLine(item);
}
It is true, but i don't want to do this way.
Thank you.
Here is a 1 liner:
array1.Intersect(array2).ToList().ForEach(i => Console.WriteLine(i));