To find a sequence of one array into another array [closed] - c#

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 8 years ago.
Improve this question
I have two array objects
int[] arr1= new int[] {1,2,5,6,7,9,3,5,6,7}
int[] arr2 = new int[] {5,6,7}
Now, how to find the no of occurrences of arr2 in arr1?

Perhaps not very elegant, but it should work.
This selects all subarrays in a having the same length of b, and checks how many are equal to b.
int[] a = {1, 2, 3, 4, 5};
int[] b = {2, 3};
int count = 0;
int bl = b.Length;
for (int i = 0; i <= a.Length - bl; i++)
{
var suba = a.Skip(i).Take(bl);
if (suba.SequenceEqual(b))
count++;
}
N.B.: this solution considers overlapping subarrays, therefore if a = {2, 2, 2} and b = {2, 2}, the count will be 2.

you may use arr2.Intersect(arr1).Count()
So in your case it will return 3, as 3 elements in arr2 are present in arr1.
If this is not what you're asking for, please clarify.

Use the intersect. This code snippet will solve your problem and print all duplicates, and show the count of each in arr1. Note that I am also using the Linq Distinct(), so that when looping common occurrences, I only check once and not more.
int[] arr1= new int[] {1,2,5,6,7,9,3,5,6,7};
int[] arr2 = new int[] {5,6,7};
var listCommon = arr1.AsEnumerable().Where(arr2.AsEnumerable().Contains);
foreach (var x in listCommon.Distinct()) {
var numberOfOccurencesInArr1 = arr1.Where(y => y == x).Count();
Console.WriteLine(x + " is : " + numberOfOccurencesInArr1.ToString() + " times in arr1");
}
Console.ReadLine();
See MSDN for more information; http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect(v=vs.110).aspx
The listCommon will be the number of common items in both arrays.

Try This
string result = string.Empty; ;
int[] arr1 = new int[] { 1, 2, 5, 6, 7, 9, 3, 5, 6, 7 };
int[] arr2 = new int[] { 5, 6, 7 };
int count = arr2.Intersect(arr1).Count();
if (count == arr2.Length)
{
result = "Found";
}
else
{
result = "Not Found";
}

If you want to count how many occurences of an integer of the second array are present in the first array then you could write
int[] arr1 = new int[] {1,2,5,6,7,9,3,5,6,7};
int[] arr2 = new int[] {5,6,7};
Dictionary<int, int> counter = new Dictionary<int, int>();
foreach(int x in arr1)
{
if(arr2.Contains(x))
{
if(counter.ContainsKey(x))
counter[x]++;
else
counter[x] = 1;
}
}
foreach(KeyValuePair<int, int> kvp in counter)
Console.WriteLine("Key=" + kvp.Key.ToString() + " is present " + kvp.Value.ToString() + " times");

int[] arr1 = new int[] { 1, 2, 5, 6, 7, 9, 3, 5, 6, 7 };
int[] arr2 = new int[] { 5, 6, 7 };
how to find the no of occurrences of arr2 in arr1?
If you are expecting the result to be 2. Since the 5,6,7 appears twice in the arr1
try this
var res = arr1.Where(x => arr2.Contains(x)).Count()/arr2.Count();

Try this one:
var results = (from a1 in arr1
join a2 in arr2
on a1 equals a2
group arr1 by a1 into Group
select new
{
Number = Group.Key,
Times = Group.Count()
});
foreach(var result in results)
Console.WriteLine(result.Number+" "+result.Times);
Please check this solution using the following fiddle .NET fiddle

You can use ToLookup on the ints in the first count which are also in the second array. Then you just have to take the min-count of all groups since that's the greatest intersection:
var subsetGroups = arr1.Where(i1 => arr2.Contains(i1)).ToLookup(i => i);
int minGroupCount = 0;
// check if all integers from the array are in the first at all
if(arr2.All(i => subsetGroups.Contains(i)))
{
minGroupCount = subsetGroups.Min(g => g.Count()); // 2
}
Note that this approach doesn't care about the order and it also doesn't care about the number of duplicates in the second array. This might be desired or not.

As you input is arrays you can use indexing to effectively count the number of occurrences of the subarrays in the main array:
var count = 0;
for (var i = 0; i < arr1.Length - arr2.Length + 1; i += 1) {
if (arr1[i] != arr2[0])
continue;
var isSubarray = true;
for (var j = 0; j < arr2.Length; ++j)
if (arr1[i + j] != arr2[j]) {
isSubarray = false;
break;
}
if (isSubarray)
count += 1;
}
The result will be 2 because 567 is found two times in 1256793567.
If the subarray can "overlap" itself (e.g. 11 in 111) all "overlaps" will be counted (e.g. the result will be 2 for this example). If that is not the intention you simply have to advance the index i at the end of the main loop to skip the found subarray.

Related

Have N elemnts of sum of a list and repeat until the end of the list [closed]

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 2 years ago.
Improve this question
I'm facing a problem when trying to play with C# list. Currently I have a list of integer. And I'm looking for a way to sum up every 5 integer, until the end of the list.
For example I have a list like this:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
How to sum up every N elements (let's say 5) in the list and become:
[ 15, 40 ]
FYI, the list consist of hundred and thousand int elements.
Thanks.
Since noone's mentioned the simple way yet...
Note that if the input is not divisible by the group size, any extra elements will be ignored. For example, if the input is 11 elements, and the group size is 5, this will give 2 sums of 5 elements and then ignore the 11th input element.
public static int[] SumEvery(int[] input, int groupSize)
{
// The number of groups we have
int numGroups = input.Length / groupSize;
int[] result = new int[numGroups];
// For each group of numbers...
for (int group = 0; group < numGroups; group++)
{
// ... loop through each number in that group, adding to the sum ...
int sum = 0;
for (int i = 0; i < groupSize; i++)
{
sum += input[group * groupSize + i];
}
// ... then store that sum in our array of results
result[group] = sum;
}
return result;
}
int[] input = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] result = SumEvery(input, 5);
If you want to stream the results as an IEnumerable<int>, rather than collecting them all into an array, then:
public static IEnumerable<int> SumEvery(int[] input, int groupSize)
{
int numGroups = input.Length / groupSize;
for (int group = 0; group < numGroups; group++)
{
int sum = 0;
for (int i = 0; i < groupSize; i++)
{
sum += input[group * groupSize + i];
}
yield return sum;
}
}
You could do this with a bit of Linq
var ints = new []{1,2,3,4,5,6,7,8,9,10};
var result = ints.Select((x,i) => new{Num = x,Index = i}) // Project to a list with the index and number
.GroupBy (i => i.Index/5) // group by the int division by 5
.Select(g => g.Sum(a => a.Num)); // sum the numbers in each group
Live example: https://dotnetfiddle.net/BabP5N
Note that is is by far the least efficient way - and with a large data set will not perform well. But for a small dataset will work fine. This code is possibly the clearest interpretation of the problem.

How do I delete all elements in an int array that exist in another int array?

In my C# program, I have an int array containing a set of integers and occasionally duplicates of those integers. I want to create an array that only contains the numbers that exist as duplicates in the initial array, but in itself contains no duplicates. Based on my newbie understanding of C# I thought that the following code would do the trick:
int a = 9;
int b = 6;
int c = 3;
int index = 0;
int[] mltpls = new int[a + b + c];
while (a > 0)
{
mltpls[index] = 2 * a;
a -= 1;
index += 1;
}
while(b > 0)
{
mltpls[index] = 3 * b;
b -= 1;
index += 1;
}
while(c > 0)
{
mltpls[index] = 5 * c;
c -= 1;
index += 1;
}
int[] mltpls_unique = mltpls.Distinct().ToArray();
int[] mltpls_dplcts = mltpls.Except(mltpls_unique).ToArray();
Console.WriteLine(mltpls_dplcts);
//EDIT
//By running the following code I can write out all numbers in "mltpls"
for (int i = 0; i < mltpls.Length; i++)
{
Console.Write(mltpls[i] + ", ");
}
/*If I try to run equivalent code for the "mltpls_dplcts" array nothing
only a blank line is displayed.*/
When I run this goal my the final result of my console application is a blank row. My interpretation of this is that the array mltpls_dplcts is empty or that I'm incorrectly going about printing the array.
How do get only the duplicate values from an array?
My interpretation of this is that the array mltpls_dplcts is empty or that I'm incorrectly going about printing the array.
Both interpretations are correct
Distinct will return every item that is at least once present in mltps. If you now apply Except you get nothing because all items that are in mltpls_unique are also present in mltps. The items in the array are compared by value, so for Except it does not matter whether a number occurs multiple times in the other array. If it is there once it will not return the number. So you get an empty array.
Furthermore you cannot simply shove an entire array into Console.WriteLine. Either use a loop or String.Join to print the content:
Console.WriteLine(String.Join(" ",mltpls_dplcts));
Solution: You can solve it using a good old loop approach ;)
int[] mltpls_unique = mltpls.Distinct().ToArray();
// The amount of duplicates is the difference between the original and the unique array
int[] mltpls_dplcts = new int[mltpls.Length-mltpls_unique.Length];
int dupCount = 0;
for (int i = 0; i < mltpls.Length; i++)
{
for (int j = i+1; j < mltpls.Length; j++)
{
if (mltpls[i] == mltpls[j])
{
mltpls_dplcts[dupCount] = mltpls[i];
dupCount++;
}
}
}
Output: 18 12 10 6 15
You cannot print the array directly. You need to loop and print one by one:
foreach (var element in mltpls_dplcts)
{
Console.WriteLine(element);
}
You can get array of distinct duplicates like this:
var duplicates = mltpls.GroupBy(o => o)
.Where(g => g.Count() > 1)
.Select(g => g.First()).ToArray();
To get new array that contains only the elements from the original one that are not in the second array you can use:
var newArr = mltpls.Except(duplicates).ToArray();
It is not proper way to find duplicates. You can determine the duplicates by using GroupBy and print them to console like this;
var mltpls_dplcts = mltpls.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x.Key).ToArray();
foreach (var duplicate in mltpls_dplcts)
{
Console.WriteLine(duplicate);
}
Also, If it isn't must to use Array for you, I suggest you to use List<int>.
Updated question from OP:
How do get only the duplicate values from an array?
var arr1 = new[] {1, 2, 4, 4, 5, 5, 5, 5, 6, 7, 1};
var duplicates = arr1.ToLookup(_ => _, _ => _).Where(_ => _.Count()>1).Select(_ => _.Key).ToArray();
// duplicates is now { 1, 4, 5 }
Original question from OP:
How do I delete all elements in an int array that exist in another int array in C#?
var arr1 = new[] {1, 2, 4, 5, 6, 7};
var arr2 = new[] {4, 5};
var hash = new HashSet<int>(arr1);
hash.ExceptWith(arr2);
var filteredArray = hash.ToArray();
// filteredArray is now { 1, 2, 6, 7 }

How to get all indices of the highest number in an array? [duplicate]

This question already has answers here:
c# Array.FindAllIndexOf which FindAll IndexOf
(10 answers)
Closed 5 years ago.
For example, I have an array
arr[5] = {1, 5, 2, 3, 5}
and the highest number is obviously 5.
My question is how do I get both the indices of the highest number (which is 5).
The expected result is 1 and 4.
var arr = new int[] { 1, 5, 2, 3, 5 };
int max = arr.Max();
List<int> indexes = new List<int>();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == max)
indexes.Add(i);
}
int highindex = indexes.LastOrDefault();
Thats how you get all indexes of your highest number
var numbers = new int[] { 1, 5, 2, 3, 5 };
int max = numbers.Max();
var indexes = numbers.Select((c, i) => new
{
character = c, index = i
})
.Where(list => list.character == max)
.ToList();
You can use a LINQ query to find the numbers and index that equal the maximum number :
var arr=new[] {1, 5, 2, 3, 5};
var max = arr.Max();
var indexes= arr.Select( (n,idx)=>n==max?idx:-1)
.Where(idx=>idx!=-1)
.ToArray();
This will return {1,4}.
This query uses the Enumerable.Select overload that provides the index of the current element and returns that index if the number is equal to the maximum.
The original title was a bit confusing - how to find the two largest values. You can use a similar query to get the N largest values by selecting the value and index, ordering the results and taking first N items:
var indexes = arr.Select( (val,idx)=> (val:val,idx:idx) )
.OrderByDescending(p=>p.val)
.Take(2)
.Select(p=>p.idx);
This query uses C# 7 tuples to hold the intermediate results and give them a name

Find duplicate in Array with single loop

The question is there is and unsorted array and the maximum value should be smaller than the length. I have to find the duplicate record in the array. The condition is to use a loop only once. This is what i have achieved so far. I wanted to know if there was any other approach through which i can achieve this.
int[] Arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };
int[] Arr2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < Arr.Length; i++)
{
if (Arr2[Arr[i]] == 0)
{
Arr2[Arr[i]] = Arr[i];
}
else
{
Console.WriteLine("duclicate found");
}
}
Use any Set implementation, say HashSet<T>, e.g.
HashSet<int> hs = new HashSet<int>();
int[] Arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };
foreach (item in Arr)
if (hs.Contains(item)) {
Console.WriteLine("duplicate found");
// break; // <- uncomment this if you want one message only
}
else
hs.Add(item);
Edit: since hs.Add returns bool a shorter and more efficient code can be put:
HashSet<int> hs = new HashSet<int>();
int[] Arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };
foreach (item in Arr)
if (!hs.Add(item)) {
Console.WriteLine("duplicate found");
// break; // <- uncomment this if you want one message only
}
Since you have this condition :
The question is there is and unsorted array and the maximum value should be smaller than the length.
Also assuming only positive numbers, which in your example applies
This can be done using O(n) time and O(1) space without using any LINQ, Dictionary, Hashing etc.
int[] arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };
for (int i = 0; i < arr.Length; i++)
{
if (arr[Math.Abs(arr[i])] >= 0)
arr[Math.Abs(arr[i])] = -arr[Math.Abs(arr[i])];
else
Console.WriteLine("Duplicate found " + Math.Abs(arr[i]).ToString() + "\n");
}
This is the Element Distinctness Problem.
This problem cannot be solved strictly linearly without additional space.
The two common approaches to solve the problem are:
Using a HashSet - populate it while iterating and abort if you find a match - O(n) time on average and O(n) space
Sort and iterate, after the array is sorted, duplicates will be adjacent to each other and easy to detect. This is O(nlogn) time and very little extra space.
The fastest way to obtain all duplicates, using LINQ, is this:
var duplicates = Arr.GroupBy(s => s).SelectMany(d => d.Skip(1));
This will return an IEnumerable of all duplicate elements in Arr, and you can use the following check to contain if there were any duplicates:
if (duplicates.Any())
{
// We have a duplicate!
}
This will work if only array a[] contains numbers in range [0,n-1] {as in your question} and n is not very large to avoid integer range overflow .
for(i=0;i<n;i++)
{
if(a[a[i]%n]>=n)
**duplicate is a[i]** !
else
a[a[i]%n]+=n;
}
Time complexity : O(N)
Space complexity : O(1) !
try this code using LINQ
int[] listOfItems = new[] { 4, 2, 3, 1, 6, 4, 3 };
var duplicates = listOfItems
.GroupBy(i => i)
.Where(g => g.Count() > 1)
.Select(g => g.Key);
foreach (var d in duplicates)
Console.WriteLine("The duplicate is "+d);

Finding the last index of an array

How do you retrieve the last element of an array in C#?
LINQ provides Last():
csharp> int[] nums = {1,2,3,4,5};
csharp> nums.Last();
5
This is handy when you don't want to make a variable unnecessarily.
string lastName = "Abraham Lincoln".Split().Last();
With C# 8:
int[] array = { 1, 3, 5 };
var lastItem = array[^1]; // 5
The array has a Length property that will give you the length of the array. Since the array indices are zero-based, the last item will be at Length - 1.
string[] items = GetAllItems();
string lastItem = items[items.Length - 1];
int arrayLength = array.Length;
When declaring an array in C#, the number you give is the length of the array:
string[] items = new string[5]; // five items, index ranging from 0 to 4.
New in C# 8.0 you can use the so-called "hat" (^) operator! This is useful for when you want to do something in one line!
var mystr = "Hello World!";
var lastword = mystr.Split(" ")[^1];
Console.WriteLine(lastword);
// World!
instead of the old way:
var mystr = "Hello World";
var split = mystr.Split(" ");
var lastword = split[split.Length - 1];
Console.WriteLine(lastword);
// World!
It doesn't save much space, but it looks much clearer (maybe I only think this because I came from python?). This is also much better than calling a method like .Last() or .Reverse() Read more at MSDN
Edit: You can add this functionality to your class like so:
public class MyClass
{
public object this[Index indx]
{
get
{
// Do indexing here, this is just an example of the .IsFromEnd property
if (indx.IsFromEnd)
{
Console.WriteLine("Negative Index!")
}
else
{
Console.WriteLine("Positive Index!")
}
}
}
}
The Index.IsFromEnd will tell you if someone is using the 'hat' (^) operator
Use Array.GetUpperBound(0). Array.Length contains the number of items in the array, so reading Length -1 only works on the assumption that the array is zero based.
To compute the index of the last item:
int index = array.Length - 1;
Will get you -1 if the array is empty - you should treat it as a special case.
To access the last index:
array[array.Length - 1] = ...
or
... = array[array.Length - 1]
will cause an exception if the array is actually empty (Length is 0).
Also, starting with .NET Core 3.0 (and .NET Standard 2.1) (C# 8) you can use Index type to keep array's indexes from end:
var lastElementIndexInAnyArraySize = ^1;
var lastElement = array[lastElementIndexInAnyArraySize];
You can use this index to get last array value in any length of array. For example:
var firstArray = new[] {0, 1, 1, 2, 2};
var secondArray = new[] {3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5};
var index = ^1;
var firstArrayLastValue = firstArray[index]; // 2
var secondArrayLastValue = secondArray[index]; // 5
For more information check documentation
The following will return NULL if the array is empty, else the last element.
var item = (arr.Length == 0) ? null : arr[arr.Length - 1]
say your array is called arr
do
arr[arr.Length - 1]
Is this worth mentioning?
var item = new Stack(arr).Pop();
Array starts from index 0 and ends at n-1.
static void Main(string[] args)
{
int[] arr = { 1, 2, 3, 4, 5 };
int length = arr.Length - 1; // starts from 0 to n-1
Console.WriteLine(length); // this will give the last index.
Console.Read();
}
static void Main(string[] args)
{
int size = 6;
int[] arr = new int[6] { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i < size; i++)
{
Console.WriteLine("The last element is {0}", GetLastArrayIndex(arr));
Console.ReadLine();
}
}
//Get Last Index
static int GetLastArrayIndex(int[] arr)
{
try
{
int lastNum;
lastNum = arr.Length - 1;
return lastNum;
}
catch (Exception ex)
{
return 0;
}
}
This is simplest and works on all versions.
int[] array = { 1, 3, 5 };
int last = array[array.Length - 1];
Console.WriteLine(last);
// 5

Categories