This question already has answers here:
How to Count Duplicates in List with LINQ
(7 answers)
Closed 9 years ago.
I have 6 numbers in array .
string[] list = { "1", "1", "2","2","1","3" };
I want result like this. please help.
"1" = 3
"2" = 2
"3" = 1
var itemCounts = list.GroupBy(l => l)
.Select(g => new { key = g.Key, count = g.Count()});
Assuming your numbers in SearchArray >0. Here is an alternative approach
You can also write a function
1) find Max - One Loop
for( int i=0;i<searchArray.length;i++){
if (searchArray[i]>max) max=searchArray[i];
}
2) Initialize an Array[Max+1]= 0
3) Loop thru each item and increment the size in Array
for( int i=0;i<searchArray.length;i++){
Array[searchArray[i]]++;
}
Related
This question already has answers here:
Subset of Array in C#
(9 answers)
Closed 1 year ago.
Say, I have a list and I want to get a portion of it. Say I have 1000 data from 0 to 999. Then I want to get from "index1" to "index2."
Sample data is :
[0] = 100
[1] = 1520
....
[900] = 8975
....
[998] = 10
[999] = 4875
Say for example I want to get values from index 900 to index 998. So the value return should be:
[0] = 8975
.....
[998] = 10
How to do that in LINQ?
You can use skip and take for that
List<int> list= new List<int>;
list.Skip(900).Take(100);
https://www.codingame.com/playgrounds/213/using-c-linq---a-practical-overview/skip-and-take
Or, you can use the GetRange method
List<int> list= new List<int>;
list.GetRange(900, 100); // Retrieves 100 items starting with index #900
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.getrange?redirectedfrom=MSDN&view=net-6.0#System_Collections_Generic_List_1_GetRange_System_Int32_System_Int32_
This question already has answers here:
Check for missing number in sequence
(14 answers)
Closed 4 years ago.
I have a list of numbers:
List<int> lstActive = new List<int>{1,6,7,8,10};
I want to get the numbers that does not exit in the above list and less than 10
e.g.
private List<int> GetInactive(List<int> lstActive, int MaxValue)
{
//To Do
}
Then:
List<int> lstInactive = GetInactive(lstActive, 10)
the result should be:
{2,3,4,5,9}
How can I do this ?
Enumerable.Range(0, maxValue).Where(n => !lstActive.Contains(n))
If perf is an issue, make a hashset:
var hs = new HashSet<int>(lstActive);
Enumerable.Range(0, maxValue).Where(n => !hs.Contains(n))
Try This:
List<int> lstActive = new List<int>(new int[]{1,6,7,8,10});
Enumerable.Range(1, 10).ToList().Except(lstActive).Dump();
https://dotnetfiddle.net/hyiAhs
This question already has answers here:
Compare List and return matches in c#
(3 answers)
Closed 6 years ago.
I have two arrays
List<int> a
List<int> b
List<int> matches
And I need to put all matches in a third (match) array so that I can print that out...
I can print out both a and b like so.
a.Sort();
label1.Text = "";
foreach (int x in a)
label1.Text += x + " , ";
a.Clear();
And so on for "b"
but how to compare the two and only take the matching integers, put them in "matching" array and print them out the same way?
You could use a linq query to get values that are in both lists...
List<int> a = new List<int> {1,2,3};
List<int> b = new List<int> {2,4,6,3};
var matches = a.Intersect(b);
// Create comma-separated string of matching values...
string output = string.Join(",", matches);
This question already has answers here:
How to replace list item in best way
(12 answers)
Closed 5 years ago.
How do I replace a value in a collection list at the same location?
0 = cat
1 = dog
2 = bird
replace 2 with snail?
Do you mean:
yourCollection[2] = "Snail";
In a bigger List<T> collection, you would like to find index to replace...with:
var i = animals.FindIndex(x => x == "Dog");
animals[i] = "Snail";
List<string> animals = new List<string>();
animals.Add("cat");
animals.Add("dog");
animals.Add("bird");
animals.Add("fish");
animals.RemoveAt(2);
animals.Insert(2, "snail");
This question already has answers here:
Determine if a sequence contains all elements of another sequence using Linq [duplicate]
(4 answers)
Closed 8 years ago.
Suppose i have the following
var mustHave = new int [] { 1, 2 }
and a table named db.Numbers
public class Number
{
public int id;
public int number;
}
populated with
Numbers.add(new Number(){id=8,number=1});
Numbers.add(new Number(){id=8,number=2});
Numbers.add(new Number(){id=8,number=3});
Numbers.add(new Number(){id=9,number=1});
Numbers.add(new Number(){id=9,number=3});
I want to get all ids which are associated to all numbers in the mustHave variable. For example The query must return id=8 but not id=9. The real case is much more complicated and does the query against a database with Linq-To-Sql.
Try this:
var numbers = Numbers.FindAll(n=>n.id=8);
var mustHave= new int[numbers.Count][2];
int counter = 0;`
foreach(var number in numbers)
{
mustHave[counter][0]=number.id;
mustHave[counter][1]=number.number;
counter++;
}
Well if you're doing something in LINQtoSQL with a DataContext, try this:
var mustHave = new int [] { 1, 2 };
var items =
from x in DataContext.SomeTable
where mustHave.Contains(x.SomeProperty)
select x;