so I was solving some unrelated coding challenge and I needed to split an int array based on an element.
similar to how String.Split works.
for example:
var arr1 = new int [] { 3, 3, 0, 2, 4, 3, 2 };
var arr2 = new int [] { 8, 8, 5, 7, 9, 8, 7, 4, 8 };
var results1 = Split(arr1, 0);
foreach (var arr in results1)
Console.WriteLine(string.Join(",", arr));
var results2 = Split(arr2, 8);
foreach (var arr in results2)
Console.WriteLine(string.Join(",", arr));
the output here is:
3,3
2,4,3,2
5,7,9
7,4
I was surprised, I could not find any answers for this, String.Split kept coming up, but no integer!
so I wrote this:
public int[][] Split(int[] arr, int element)
{
List<int[]> arrays = new List<int[]>();
int skip = 0;
int take = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == element && take != 0)
{
arrays.Add(arr.Skip(skip).Take(take).ToArray());
skip = i + 1;
take = 0;
continue;
}
if (arr[i] != element)
take++;
if (take == 0)
skip = i + 1;
if (arr.Length - 1 == i && take != 0)
arrays.Add(arr.Skip(skip).Take(take).ToArray());
}
return arrays.ToArray();
}
this works (I think), but it is very messy and I don't like it
Any other solutions?
In general case, you can implement it (String.Split like routine but for IEnumerable<T>) as follows:
public static IEnumerable<T[]> Split<T>(IEnumerable<T> source,
T delimiter,
StringSplitOptions options = StringSplitOptions.None,
IEqualityComparer<T> comparer = null) {
if (null == source)
yield break; // Or throw new ArgumentNullException(nameof(source));
if (null == comparer)
comparer = EqualityComparer<T>.Default;
List<T> current = new List<T>();
foreach (T item in source) {
if (comparer.Equals(item, delimiter)) {
if (current.Count > 0 || !options.HasFlag(StringSplitOptions.RemoveEmptyEntries))
yield return current.ToArray();
current.Clear();
}
else
current.Add(item);
}
if (current.Count > 0 || !options.HasFlag(StringSplitOptions.RemoveEmptyEntries))
yield return current.ToArray();
}
Then
var arr1 = new int [] { 3, 3, 0, 2, 4, 3, 2 };
var results1 = Split(arr1, 0);
foreach (var arr in results1)
Console.WriteLine(string.Join(", ", arr));
public static IEnumerable<List<TValue>> SplitBy<TValue>(List<TValue> source, TValue by)
{
int start = 0;
int count = 0;
foreach (TValue item in source)
{
if (item.Equals(by))
{
List<TValue> part = source.GetRange(start, count);
start = start + count + 1;
count = 0;
if (part.Any())
{
yield return part;
}
}
else
{
count++;
}
}
List<TValue> rest = source.GetRange(start, count);
yield return rest;
}
https://dotnetfiddle.net/Widget/Preview?url=/Widget/7ORu8p
Related
Here is my code
public static int[] MoveZeroes(int[] arr)
{
// TODO: Program me
int zeroCount = 0;
int[] temp = { };
int numberItems = 0;
foreach (var a in arr)
{
if (a == 0)
{
zeroCount += 1;
}
else
{
temp[numberItems] = a;
}
numberItems += 1;
}
return new int[] { };
}
i use it like
int[] f = MoveZeroes(new int[] {1, 2, 1, 1, 3, 1, 0, 0, 0, 0});
But this is giving me error Index was outside the bounds of the array on line
temp[numberItems] = a;
how can i add items in array? what am i doing wrong ?
int[] temp = { }
This creates an array of ints that is 0 elements long. You can't insert into it because it has 0 length.
Use a List<int> and you can dynamically add to it:
public static int[] MoveZeroes(int[] arr)
{
// TODO: Program me
int zeroCount = 0;
var temp = new List<int>();
int numberItems = 0;
foreach (var a in arr)
{
if (a == 0)
{
zeroCount += 1;
}
else
{
temp.Add(a);
}
numberItems += 1;
}
return temp.ToArray();
}
I'm trying to reorder a list that essentially gets rid of duplicates. Which I have been able to do. The part that is tripping me up is, if there are duplicates I want to keep them, and maintain the order
Examples -
Input = 1,3,3,4
Output = 1,2,2,3
Input = 1,3,5,6,6
Output = 1,2,3,4,4
Any help would be appreciated.
EDIT -
Code I've attempted.
for (int i = 1; i <= numList.Count; i++)
{
if (numList[i - 1] == numList[i])
{
foreach (var item in numList.Where(x => x == numList[i - 1]))
{
testList.Add(item);
i++;
}
foreach (var item in testList)
{
numList.Add(item);
}
}
else
{
numList[i - 1] = i;
}
}
Your question has nothing to with sorting... but I think I understood what you are looking for, and the following code should provide it:
List<Int32> numbers = new List<Int32> { 1, 3, 3, 4 };
List<Int32> result = new List<Int32>(numbers.Count);
Int32 currentValue = 1;
Int32 lastNumber = numbers[0];
for (Int32 i = 0; i < numbers.Count; ++i)
{
Int32 number = numbers[i];
if (numbers[i] != lastNumber)
++currentValue;
result.Add(currentValue);
lastNumber = number;
}
foreach (Int32 number in result)
Console.WriteLine(number); // Output: 1, 2, 2, 3
Please, visit this link to try a working demo, which implements the following function:
private static List<Int32> IndexNumbers(List<Int32> input)
{
List<Int32> numbers = input.OrderBy(x => x).ToList();
List<Int32> result = new List<Int32>(input.Count);
Int32 currentValue = 1;
Int32 lastNumber = numbers[0];
for (Int32 i = 0; i < numbers.Count; ++i)
{
Int32 number = numbers[i];
if (numbers[i] != lastNumber)
++currentValue;
result.Add(currentValue);
lastNumber = number;
}
return result;
}
Something like this? Note that it is unclear whether we expect the input to be out of order or not. If we're allowed to change the input array, a simple Array.Sort(values) at the top of Rebase would ensure that aspect.
static void Main(string[] args)
{
Console.WriteLine(string.Join(", ", Rebase(1, 3, 3, 4))); // 1, 2, 2, 3
Console.WriteLine(string.Join(", ", Rebase(1, 3, 5, 6, 6))); // 1, 2, 3, 4, 4
}
static int[] Rebase(params int[] values)
{
// Array.Sort(values); // if not already sorted, and allowed to mutate
int prev = values[0];
var result = new int[values.Length];
int val = result[0] = 1;
for (int i = 1; i < values.Length; i++)
{
var current = values[i];
if (current != prev)
{
val++;
prev = current;
}
result[i] = val;
}
return result;
}
Note that you could also choose to overwrite the input array rather than create a new array to return.
Try this using LINQ:
var reindexed = numList
.OrderBy(n => n)
.GroupBy(n => n)
.SelectMany((g, i) => g
.Select(e => i + 1));
First off, figure out what the old number should match to. Distinct and OrderBy are probably you're friends here
List<int> input = new List<int> { 1, 3, 3, 4 };
var dictionary = new Dictionary<int, int>();
var index = 1;
foreach (var n in input.Distinct().OrderBy(n => n)) {
dictionary.Add(n, index++);
}
var output = input.Select(n => dictionary[n]);
I am trying to practice some c# questions and I came across a question where given an integer of array, return the array with elements that can be repeated only N times. So, if I have {1,2,3,4,1,1,2,3} and my N=2, my result should be {1,2,3,4,1,2,3}. I tried something but I get the result as {2,3,4,1,1,2,3}.I don't want the first repeated element be removed.
This is what I have tried :
int[] intArray = {3,2,3,1,3};
int N = 2;
var list = new List<int>(intArray);
var newList = new List<int>();
for (int i = 0; i < list.Count; i++)
{
int occur = 1;
for (int j = i+1; j < list.Count; j++)
{
if (occur < N && list[i] == list[j])
{
occur++;
newList.Add(list[j]);
}
else
{
if (list[i] == list[j] )
list.Remove(list[j]);
}
}
}
foreach (var l in list)
{
Console.WriteLine(l);
}
}
}
I would really appreciate any help or guidance.
I suggest using Dictionary<T, int> to count appearances:
private static IEnumerable<T> RepeatOnly<T>(IEnumerable<T> source, int times) {
Dictionary<T, int> counts = new Dictionary<T, int>();
foreach (var item in source) {
int count;
if (counts.TryGetValue(item, out count))
counts[item] = ++count;
else
counts.Add(item, count = 1);
if (count <= times)
yield return item;
}
}
....
int[] source = new int[] { 1, 2, 3, 4, 1, 1, 2, 3 };
int[] result = RepeatOnly(source, 2).ToArray();
Console.Write(string.Join(Environment.NewLine, result));
I'd use a dictionary to keep track of how many times each integer has been encountered:
int[] intArray = { 1, 2, 3, 4, 1, 1, 2, 3 };
int N = 2;
var lookup = new Dictionary<int, int>();
LIst<int> list = new List<int>();
foreach (int num in intArray)
{
if (!lookup.ContainsKey(num))
{
lookup[num] = 1;
list.Add(num);
}
else if (lookup[num]++ < N)
{
list.Add(num);
}
}
My array is below
int[] T1 = new int[]{1,5,4};
int[] T2 = new int[]{2,4,3};
int[] T3 = new int[]{7,9,1};
A result of the sequence I want it is as follows .
int[] result = {7,9,4}
I want to look for a max value same index[(e.g) 1,2,7) in array.
and Merging array is int[] result = {7,9,4}
please let me know best way
thanks
Here is a simple method for performing your desired operation.
int[] result = new int[T1.Length];
for (int i = 0; i < T1.Length; i++)
{
result[i] = new[] {T1[i], T2[i], T3[i]}.Max();
}
You'll have to add using System.Linq in order to enable the Max() extension.
EDIT
Here is a method that allows you to use any number of arrays:
static T[] MaxElements<T>(params T[][] arrays) where T : IComparable<T>
{
return Enumerable.Range(0, arrays[0].Length)
.Select(i => Enumerable.Range(0, arrays.Length)
.Select(a => arrays[a][i]).Max()).ToArray();
}
You'll have to add the using System.Linq; for this one too. You'll have to make sure that all the elements passed in have the same lengths else you will run into an IndexOutOfRangeException.
It can be used as follows;
var result = MaxElements(T1, T2, T3);
Here is what you need:
static void Main(string[] args)
{
GetMaxArray(new[] { 1, 5, 4 }, new[] { 2, 4, 3 }, new[] { 7, 9, 1 });
}
private static void GetMaxArray(params int[][] m)
{
var length = m.First().Length;
var r = new int[length];
for (var i = 0; i < length; i++)
r[i] = m.Select(a => a[i]).Max();
}
public int[] GetMaxValueArray(List<int[]> arrayList)
{
int[] result = null;
int maxArraySize;
int maxCurrentArrayItemValue;
if (arrayList != null && arrayList.Count > 0)
{
maxArraySize = 0;
foreach (int[] array in arrayList)
if (array.Length > maxArraySize)
maxArraySize = array.Length;
result = new int[maxArraySize];
for (int i = 0; i < maxArraySize; i++)
{
maxCurrentArrayItemValue = 0;
foreach (int[] array in arrayList)
if (i < array.Length)
if (array[i] > maxCurrentArrayItemValue)
maxCurrentArrayItemValue = array[i];
result[i] = maxCurrentArrayItemValue;
}
}
return result;
}
I want to create a method that I can send two arrays (that will be containing ints). These arrays wont necessarily be equally long. for example first array could have an index of 15 while the second array has an index of 12. in that case I want to add array1 and array2 for the first 12 then just get the value of array1 for the last 3.
I thought something like this:
int[] ArrTotal(int[] array1, int[] array2)
{
int[] total = new int[15];
for (int i = 0; i < 15; i++)
{
if (array1[i] != null && array2[i] != null)
{
total[i] = array1[i] + array2[i];
}
else if(array1[i] != null)
{
total[i] = array1[i];
}
else if (array2[i] != null)
{
total[i] = array2[i];
}
else
{
total[i] = 0;
}
}
return total;
}
Problem is that I can't check and see if an int array is null. I read something about doing an:
If(i < array1.Length)
but that does not seem to work either, it says it will always be true in my case.
Am I on the right track at all or is there some major flaw I'm missing? :)
How about:
int[] ArrTotal(int[] a, int[] b)
{
if (a == null || b == null)
{
return (int[])(a ?? b).Clone();
}
int length = Math.Max(a.Length, b.Length);
int[] result = new int[length];
for (int i = 0; i < length; i++)
{
int sum = 0;
if (a.Length > i) sum += a[i];
if (b.Length > i) sum += b[i];
result[i] = sum;
}
return result;
}
Try to check lengths of both arrays before:
int length = (array1.Length < array2.Length ? array1.Length : array2.Length);
Then iterate and assign only to array indeces from 0 to length of the shorter array - 1:
for (int i = 0; i < 15; i++)
if (i < length)
newArray[i] = array1[i] + array2[i];
else
newArray[i] = 0;
int[] a1 = new int[] { 1, 2, 3, 2, 3, 1 };
int[] a2 = new int[] { 1, 2, 3, 2, 3, 1, 3, 2, 3 };
List<int> r = new List<int>();
bool a1_longer = (a1.Length > a2.Length);
int length_diff = Math.Abs(a1.Length - a2.Length);
int length = (a1_longer ? a2.Length : a1.Length);
for (int i = 0; i < length; i++) r.Add(a1[i] + a2[i]);
for (int i = 0; i < length_diff; i++) {
r.Add(a1_longer ? a1[length + i] : a2[length+i]);
}
r.ToArray();
You may use Linq for it:
int[] ArrTotal(int[] array1, int[] array2)
{
return Enumerable.Repeat(0, Math.Max(array1.Length,array2.Length))
.Select((a, index) => a +
((array1.Length > index) ? array1[index] : 0)+
((array2.Length > index) ? array2[index] : 0))
.ToArray();
}
Using Linq you can do this (which will handle null arrays). You will need a using System.Linq at the top of the source code file:
int[] ArrTotal(int[] array1, int[] array2)
{
if ((array1 == null) && (array2 == null))
return new int[0]; // Zero length array - put some other number here if you need!
else if (array1 == null)
return (int[])array2.Clone(); // Result will just be a copy of the non-null array.
else if (array2 == null)
return (int[]) array1.Clone(); // Result will just be a copy of the non-null array.
else
{
int skip = Math.Min(array1.Length, array2.Length);
return Enumerable
.Zip(array1, array2, (i1, i2) => i1 + i2)
.Concat(array1.Skip(skip))
.Concat(array2.Skip(skip))
.ToArray();
}
}