Find first free element - c#

I looking for query which return first number not available in list
int[] list = new int[] { 1,4,2,5,6,7 };
For above example I expect to have result 3.

Perhaps something like this:
int result = Enumerable.Range(1, list.Length)
.Where(i => !list.Contains(i))
.FirstOrDefault();
This will return 0 if list contains all integers from 1 to n.

var first = Enumerable.Range(1, list.Max()).Except(list).First();

Related

Ordering Linq list by array elements

I have below code -
var refNosToOrder = new int[9] {1,2,3,4,5,6,7,8,9}
var orderedList = lst.OrderBy(x=>x.RefNo==7)
.ThenBy(y=> refNosToOrder.Contains(y.RefNo)).ToList();
lst is list of class object containing int property - RefNo : i.e. List<SampleClass>
class SampleClass
{
public int RefNo {get;set;}
}
lst contains all the unsorted data of RefNo:
lst = 2,4,6,9,7,5,8,1,3
What I want to do -
First I want to order lst by keeping first element as - 7; then for the rest of the list, it should be ordered as the array refNosToOrder
i.e. Final output I am expecting to be -
7,1,2,3,4,5,6,8,9
With the above code -
var orderedList = lst.OrderBy(x=>x.RefNo==7)
.ThenBy(y=> refNosToOrder.Contains(y.RefNo)).ToList();
It is giving - 2,4,6,9,7,5,8,1,3 i.e. this code is not at all ordering the list.
Contains returns a boolean of whether an element is in a list or not, which won't be very helpful here. Instead, you could sort by the index of that element:
var orderedList =
lst.OrderBy(x => x.RefNo != 7)
.ThenBy(y => Array.IndexOf(refNosToOrder, y.RefNo))
.ToList();
EDIT:
Following up on Jeroen Mostert's comment, this sorting has quadratic complexity. For large refNosToOrder it may be more efficient to first convert the array to a dictionary of orders and then use it for the sorting:
var orderDict =
Enumerable.Range(0, refNosToOrder.Length).ToDictionary(i => refNosToOrder[i]);
var orderedList =
lst.OrderBy(x => x.RefNo != 7).ThenBy(y => orderDict[y.RefNo]).ToList();

How to find the total redundancy in the numerical list

If I have a list
List<int> mylist = new List<int>();
mylist.Add(15);
mylist.Add(15);
mylist.Add(10);
mylist.Add(10);
mylist.Add(10);
If you want Count() which is equal to "10"
Give me the number 3
It sounds like you probably just want to use LINQ:
int target = 10; // Or whatever
int count = myList.Count(x => x == target);
You could create a function that takes the number and the list, then use LINQ to filter numbers and count:
public int countOccurrences(int whichNumber, List<int> myList){
return myList.Where(x => x == whichNumber).Count();
}

Check if at least two element in array are equal

Is there a way to check that at least one element appears more than once in an array are equal without sequentially comparing every element.
Example: In int[] array1 = { 1, 3, 4, 2, 4 }; the element 4 appears twice.
You just need to check if there is any difference between original array and it's Distinct() version.
var result = (array1.Count()-array1.Distinct().Count())>0;
You can use Distinct() method to get unique values of array and compare length with original array
int[] arrayDistinctElements = array1.Distinct().ToArray();
if(arrayDistinctElements.length == array1.length)
{
//All unique elements
}
else
{
//Duplicates were present
}
This might do the trick for you
var duplicates = array1.GroupBy(p => p).Where(g => g.Count() > 1).Select(g => g.Key);
variable duplicates contains the list of repeated items
EDIT
If you want to the return value to be Boolean than
var duplicates = array1.GroupBy(p => p).Where(g => g.Count() > 1).Select(g => g.Key).Count() > 0;
You can also do something like this
if(array1.Distinct().Count() != array1.Count())
return true; /// You have duplicates in the array
else
return false; /// All the elements in the array are different
You can use the following code:
var isExisted = list.Count(item => item.Param == "test") >= 2;
Sample solution in your case: https://dotnetfiddle.net/1y4w9K

Get Count in List of instances contained in a string

I have a string containing up to 9 unique numbers from 1 to 9 (myString) e.g. "12345"
I have a list of strings {"1"}, {"4"} (myList) .. and so on.
I would like to know how many instances in the string (myString) are contained within the list (myList), in the above example this would return 2.
so something like
count = myList.Count(myList.Contains(myString));
I could change myString to a list if required.
Thanks very much
Joe
I would try the following:
count = mylist.Count(s => myString.Contains(s));
It is not perfectly clear what you need, but these are some options that could help:
myList.Where(s => s == myString).Count()
or
myList.Where(s => s.Contains(myString)).Count()
the first would return the number of strings in the list that are the same as yours, the second would return the number of strings that contain yours. If neither works, please make your question more clear.
If myList is just List<string>, then this should work:
int count = myList.Count(x => myString.Contains(x));
If myList is List<List<string>>:
int count = myList.SelectMany(x => x).Count(s => myString.Contains(s));
Try
count = myList.Count(s => s==myString);
This is one approach, but it's limited to 1 character matches. For your described scenario of numbers from 1-9 this works fine. Notice the s[0] usage which refers to the list items as a character. For example, if you had "12" in your list, it wouldn't work correctly.
string input = "123456123";
var list = new List<string> { "1", "4" };
var query = list.Select(s => new
{
Value = s,
Count = input.Count(c => c == s[0])
});
foreach (var item in query)
{
Console.WriteLine("{0} occurred {1} time(s)", item.Value, item.Count);
}
For multiple character matches, which would correctly count the occurrences of "12", the Regex class comes in handy:
var query = list.Select(s => new
{
Value = s,
Count = Regex.Matches(input, s).Count
});
try
var count = myList.Count(x => myString.ToCharArray().Contains(x[0]));
this will only work if the item in myList is a single digit
Edit: as you probably noticed this will convert myString to a char array multiple times so it would be better to have
var myStringArray = myString.ToCharArray();
var count = myList.Count(x => myStringArray.Contains(x[0]));

Intersection of 6 List<int> objects

As I mentioned in the title I've got 6 List objects in my hand. I want to find the intersection of them except the ones who has no item.
intersectionResultSet =
list1.
Intersect(list2).
Intersect(list3).
Intersect(list4).
Intersect(list5).
Intersect(list6).ToList();
When one of them has no item, normally I get empty set as a result. So I want to exclude the ones that has no item from intersection operation. What's the best way to do that?
Thanks in advance,
You could use something like this:
// Your handful of lists
IEnumerable<IEnumerable<int>> lists = new[]
{
new List<int> { 1, 2, 3 },
new List<int>(),
null,
new List<int> { 2, 3, 4 }
};
List<int> intersection = lists
.Where(c => c != null && c.Any())
.Aggregate(Enumerable.Intersect)
.ToList();
foreach (int value in intersection)
{
Console.WriteLine(value);
}
This has been tested and produces the following output:
2
3
With thanks to #Matajon for pointing out a cleaner (and more performant) use of Enumerable.Intersect in the Aggregate function.
Simply, using LINQ too.
var lists = new List<IEnumerable<int>>() { list1, list2, list3, list4, list5, list6 };
var result = lists
.Where(x => x.Any())
.Aggregate(Enumerable.Intersect)
.ToList();
You could use LINQ to get all the list that are longer then 0 , and then send them to the function you've described.
Another option :
Override/Extend "Intersect" to a function that does Intersect on a list only if it's not empty , and call it instead of Intersect.

Categories