Find the mode in a list of doubles C#? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm making a program using C# that calculates measures of variation and measures of center in statistics. So the user inputs a list of numbers and it adds the numbers to a list. I want the program to be able to find the mode (If there is one) of the list and then return it. I also want it to return the modes separated by commas if there is multiple modes.
Wikipedia Mode Link

The following should work, it really is quite trivial.
(It could be optimized probably ¯\_(ツ)_/¯ )
var items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 2, 6, 2, 8, 8 };
var grouped = items.GroupBy(i => i).Select(i => new { value = i.Key, count = i.Count() });
var maxCount = grouped.Max(g => g.count);
var mode = grouped.Where(g => g.count == maxCount);

Related

How to add List's all values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
of course I can use for() {}
but is there any function which can add all values in array?
if do you know, please tell me.
thank you.
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[a.Length];
a.CopyTo(b, 0);
b.ToList().ForEach(c=>{
System.Console.WriteLine(c);
});
using System.Linq;
then...
var array = new double[] { 1.1, 2.2, 3.3 };
var sum = array.Sum(value=>value);
Console.WriteLine($"Sum is {sum}");

Percent similarity of two ranges of numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to get the percent similarity or overlap of two ranges of numbers.
For example, consider the ranges [10,1] and [5,1]. the range [5,1] covers 50% of the range [10,1], and a range [100,50] covers 0% of [10,1].
Edit: forgot to state the question. How do I do this?
I would just get the Count of the Intersection and compare it to the Count of the range we're interested in, for example:
var firstRange = Enumerable.Range(1, 10);
var secondRange = Enumerable.Range(1, 5);
var secondPercentCoverageOfFirst =
(double)firstRange.Intersect(secondRange).Count() / firstRange.Count() * 100;
Console.WriteLine($"{secondPercentCoverageOfFirst:0.00}%");
Console.Write("\nPress any key to exit...");
Console.ReadKey();
Of course this would apply to any set of comparable objects, not just ranges of consecutive numbers...
var firstSet = new List<string> { "car", "bus", "boat", "plane" };
var secondSet = new List<string> { "bicycle", "car", "boat", "motorcycle" };
var secondPercentCoverageOfFirst =
(double)firstSet.Intersect(secondSet).Count() / firstSet.Count() * 100;
Output for both cases

what is the most efficient data structure for matching specific numbers to ranges of percents? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In my program, I want to be able to match certain numbers to ranges of percentages, like: 0 would be the match of 0%, 1 would be the match of less then 10%, 2 would be the match of 10%-20%... and so forth. What is the most efficient data structure/method to do it ?
I would like to perform it in c#.
A Dictionary for this purpose could be a decent solution. The keys of the Dictionary would be the numbers and the values could be Tuples with the corresponding min and max percentages. If you want to learn the range for a number you could retrieve it's range in O(1).
You could define it as:
var numbersPercentageRanges = new Dictionary<int, Tuple<double, double>>
{
{ 0, Tuple.Create(0,0) },
{ 1, Tuple.Create(0.1,0.2)}
};
and you could retrieve the corresponding range as:
if(numbersPercentageRanges.TryGetValue(1, out var range))
{
var min = range.Item1;
var max = range.Item2;
}

C# Console Application, how to make all results equals totalResult? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How do I make the last result equal the first, second, third, and fourth result combined?
If all results are 10 points except the last one, then the last one should be 40 points. The last result is the total result.
int[] result = new int[5] { 0, 0, 0, 0, 0 };
Here you go
int[] result = { 1, 2, 3, 4, 0 };
result[result.Length - 1] = result.Take(result.Length - 1).Sum(); // 1 + 2 + 3 + 4 = 10
//result = { 1, 2, 3, 4, 10 };

LinQ How to get all the data from the range of numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am new to LinQ and I want to get all the data from a range of numbers when I pass a parameter. For an example, the range is 1-100 and I pass in 40, it should get all the data from 1-100 but not only 40. Any ideas?
Thanks
You can use Enumerable.Range which will give you range of numbers. You can use Except method to remove the numbers that you dont want.
private static int[] GetRange(int start,int inclusiveEnd,int[] except)
{
return Enumerable.Range(start, inclusiveEnd - start + 1).Except(except).ToArray();
}
Then call it in this way.
var x = GetRange(1, 100, new[] {40});
Use filtering (i.e. Where statement):
var result = Enumerable.Range(1,100).Where(x=>x != 40).ToList()
List<int> list = new List<int>();
var result = list.Where(i=> i >= 1 && i <= 100 && i != 40);
Assuming, that there is a function, that transforms a number from given range into some data:
Enumerable.Range(1, 100).Where(_ => _ != 40).Select(_ => GetData(_));
Use Except extension method From MSDN
List<int> oldList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int parameter = 2;
List<int> newList = oldList.Except(new int[] { parameter }).ToList<int>();

Categories

Resources