Compact an Array function in C# - c#

How can I make a Function to compact an array with duplicate entries?
For example!
A function that will take a sorted array of integers and return the array compacted. That is, given an array containing: 1, 2, 6, 8, 8, 8, 9, 10, 10, when the function returns, the contents of the array should be: 1, 2, 6, 8, 9, 10.
This is NOT hw. I am trying to make a function that will do this.

How about:
array = array.Distinct().ToArray();
or, as a function:
private int[] RemoveDuplicates(int[] array)
{
return array.Distinct().ToArray();
}
then call it with:
array = RemoveDuplicates(array);

extension methods:
public static T[] RemoveDuplicates<T>(this T[] array)
{
return array.Distinct().ToArray();
}
public static List<T> RemoveDuplicates<T>(this List<T> list)
{
return list.Distinct().ToList();
}
using for array:
int[] array = new[] {1, 3, 4, 3};
array = array.RemoveDuplicates();
using for list:
List<int> list = new List<int> {1, 3, 4, 3};
list = list.RemoveDuplicates();

The most straightforward way is probably with Linq
array = array.Distinct().ToArray()
However, Linq is not always the fastest approach.
If you do not wish to use Linq, you can do something like this (untested, but close)
List<int> compacted = new List<int>();
// If array is not a local variable:
// Assign to a variable to avoid re-evaluating the property every loop iteration
// Otherwise use array.Length as the loop termination condition to enable array
// bounds check elimination. Thanks #Harold for the insight
// http://blogs.msdn.com/b/clrcodegeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx
int max = array.Length;
int last = 0;
for (int i=0; i < max; i++)
{
if (i == 0)
{
compacted.Add(array[i]);
last = array[i];
}
else
{
if (array[i] != last) compacted.Add(array[i]);
last = array[i];
}
}
array = compacted.ToArray();

Related

reversing an array using methods and MessageBox.show

I'm having issue with the following code:
public int ReverseArray(int[] rArray)
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };
Array.Reverse(array);
foreach (int value in array)
{
return (value);
}
return 0;
}
private void reverseButton_Click(object sender, EventArgs e)
{
int[] input = new int[10];
int output = ReverseArray(input);
MessageBox.Show(""+ output);
}
The code is supposed to take the given array (int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };) and reverse it upon a button click; however, when I click on the reverseButton, I only get the number 8 and not the entire array. I'm sure it's the way my reversButton code is written, but I'm not sure how to fix it.
How can I fix my code to where when I click on reverseButton, the entire array will be displayed in reverse order?
You are not returning whole array back and also not iterating the array result and Array is a collection of items to you need to tell it to get each item one by one and print.
You can do something like following to make it work:
public int[] ReverseArray(int[] rArray)
{
Array.Reverse(array);
return array;
}
and in button click event you can use it:
int[] input = { 1, 2, 3, 4, 5, 6, 7, 8 };
input = ReverseArray(input);
string items = String.Join(",",input);
MessageBox.Show(items);
Because your method ReverseArray just returns one value:
public int[] ReverseArray(int[] rArray)
{
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };
Array.Reverse(array);
return array;
}
To show whole array you can combine it into some string:
int[] output = ReverseArray(input);
var message = string.Join(", ", output);
And show:
MessageBox.Show(message);

How to add data from 1d to 2d array in C#

How can I add an array to another 2D array? For example
//change this
array2d = { {1,2,3}, {4,5,6} };
//to this
array2d = { {1,2,3}, {4,5,6}, {7,8,9} };
//by adding
array1d = {7,8,9};
Is there any better way other than create a new array, which is bigger than the old one, then copy the old array to the array?
I would recommend a List<List<int>> for something like this, unless you have a reason not to.
The code for it may look something like this:
List<List<int>> my2dList = new List<List<int>>()
{
new List<int>()
{
1,
2,
3
},
new List<int>()
{
4,
5,
6
},
};
my2dList.Add(new List<int>(){7,8,9});
Alternatively, if you really want to limit each column to a length of three, consider using an inner structure with an immutable size (array of size 3, etc)
It seems like you might want to use a different data structure, a list of arrays would make this much easier.
Extended version of the code snippet posted by #VP. includes the back-conversion from List(List> to Jagged Array using Linq:
// 2d array to List
List<List<int>> ar2list = new List<List<int>>()
{
new List<int>() { 1, 2, 3 },
new List<int>() { 4, 5, 6 },
};
// adding item to List
ar2list.Add(new List<int>() { 7, 8, 9 });
// List to Jagged array conversion using Linq
int[][] _arrConcat = ar2list.Select(Enumerable.ToArray).ToArray();
Finally, pertinent to this particular use-case as requested by OP, int[][] can be easily converted to int[,] with simple for loop:
array2d = new int[_arrConcat.Length, 3];
for (int i = 0; i<_arrConcat.Length; i++)
{
for (int j = 0; j < 3; j++)
{
array2d[i, j] = _arrConcat[i][j];
}
}
where array2d is the original array with added index/content.
Hope this will help. Best regards,

Reversing order of elements in multidimensional array

I have an array with elemnents in order 1,2,3,4,5 and I would need to reverse it so it will be 5,4,3,2,1.
What about the following pseudo code? Is here not an easier way
EDIT: I Am sorry I thought multidimensional array
someclass [,] temporaryArray=new someclass [ArrayLenght,ArrayLenght];
//for each dimension then
for(int I=0;I<ArrayLenghtOfDimension;I++)
{
temporaryArray[ArrayLenghtOfDimension-I]=Array[I];
}
Array=temporaryArray;
The array base class has a Reverse() extension method built in
int[] originalArray = new int[] { 1, 2, 3, 4, 5 };
int[] reversedArray = originalArray.Reverse().ToArray();
Note that the Reverse method returns IEnumerable, so you need to call ToArray() on the result.
And if you need to just iterate over the elements in the array, then all you need is
foreach (int element in originalArray.Reverse())
Console.WriteLine(element);
Oops - Reverse is on IEnumerable, not Array, so you can use that with any collection.
IEnumerable<int> IEnumerableInt = new List<int>() { 1, 2, 3 };
int[] reversedArray2 = IEnumerableInt.Reverse().ToArray();
Yes there is fast solution exists in .net
int[] values = new int[] { 1, 2, 3, 4, 5 };
Array.Reverse(values);
Your array is reversed. so you can iterate through it
foreach (int i in values)
{
Response.Write(i.ToString());
}
the above code will display
54321
It will also work for string[], char[] or other type of arrays
Event though the Array class has Reverse methods defined:
Array.Reverse(originalArray); // original array is now reversed
If all you need to do is iterate backwards over it do the following:
for(int I= ArrayLength - 1; I >= 0; I--)
{
}
This avoid re-allocating memory for the reversed array.
Array.Reverse is the best way to do this. Do you care about order of the elements at all? If so,then you can do the following.
int[] originalArray = new int[] { 10, 2, 13, 4, 5 };
int[] descOrderedArray = originalArray.OrderByDescending(i => i).ToArray();
int[] ascOrderedArray = originalArray.OrderBy(i => i).ToArray();
For a multi-dimensional array it's the same idea
int[][] multiDimArray = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
int[][] reversedMultiArray = multiDimArray.Reverse().ToArray();
produces an array of two arrays that is: {4, 5, 6}, {1, 2, 3}

How to easily order an array from largest to smallest in Visual C#?

I have a small array of ints. I want to reorder the array from largest to smallest. Is there a method to do this?
You could use Array.Sort:
int[] array = new[] { 1, 3, 2 };
Array.Sort(array, (x, y) => y.CompareTo(x));
As far as complexity is concerned:
On average, this method is an O(n log
n) operation, where n is the Length of
array; in the worst case it is an O(n
^ 2) operation
You can do it using Array Sort & Reverse:
Array.Sort(array);
Array.Reverse(array);
Example:
[Test]
public void Test()
{
var array = new[] { 1, 3, 2 };
Array.Sort(array);
Array.Reverse(array);
CollectionAssert.AreEquivalent(new[] { 3, 2, 1 }, array);
}
You can try something like this
int[] ints = new int[] {1, 2, 3, 4, 1, 2, 3};
var sorted = ints.OrderBy(i => i);
Found at Sort array of items using OrderBy<>

How to delete an element from an array in C#

Lets say I have this array,
int[] numbers = {1, 3, 4, 9, 2};
How can I delete an element by "name"? , lets say number 4?
Even ArrayList didn't help to delete?
string strNumbers = " 1, 3, 4, 9, 2";
ArrayList numbers = new ArrayList(strNumbers.Split(new char[] { ',' }));
numbers.RemoveAt(numbers.IndexOf(4));
foreach (var n in numbers)
{
Response.Write(n);
}
If you want to remove all instances of 4 without needing to know the index:
LINQ: (.NET Framework 3.5)
int[] numbers = { 1, 3, 4, 9, 2 };
int numToRemove = 4;
numbers = numbers.Where(val => val != numToRemove).ToArray();
Non-LINQ: (.NET Framework 2.0)
static bool isNotFour(int n)
{
return n != 4;
}
int[] numbers = { 1, 3, 4, 9, 2 };
numbers = Array.FindAll(numbers, isNotFour).ToArray();
If you want to remove just the first instance:
LINQ: (.NET Framework 3.5)
int[] numbers = { 1, 3, 4, 9, 2, 4 };
int numToRemove = 4;
int numIndex = Array.IndexOf(numbers, numToRemove);
numbers = numbers.Where((val, idx) => idx != numIndex).ToArray();
Non-LINQ: (.NET Framework 2.0)
int[] numbers = { 1, 3, 4, 9, 2, 4 };
int numToRemove = 4;
int numIdx = Array.IndexOf(numbers, numToRemove);
List<int> tmp = new List<int>(numbers);
tmp.RemoveAt(numIdx);
numbers = tmp.ToArray();
Edit: Just in case you hadn't already figured it out, as Malfist pointed out, you need to be targetting the .NET Framework 3.5 in order for the LINQ code examples to work. If you're targetting 2.0 you need to reference the Non-LINQ examples.
int[] numbers = { 1, 3, 4, 9, 2 };
numbers = numbers.Except(new int[]{4}).ToArray();
You can also convert your array to a list and call remove on the list. You can then convert back to your array.
int[] numbers = {1, 3, 4, 9, 2};
var numbersList = numbers.ToList();
numbersList.Remove(4);
The code that is written in the question has a bug in it
Your arraylist contains strings of " 1" " 3" " 4" " 9" and " 2" (note the spaces)
So IndexOf(4) will find nothing because 4 is an int, and even "tostring" would convert it to of "4" and not " 4", and nothing will get removed.
An arraylist is the correct way to go to do what you want.
I posted my solution here.
This is a way to delete an array element without copying to another array - just in frame of the same array instance:
public static void RemoveAt<T>(ref T[] arr, int index)
{
for (int a = index; a < arr.Length - 1; a++)
{
// moving elements downwards, to fill the gap at [index]
arr[a] = arr[a + 1];
}
// finally, let's decrement Array's size by one
Array.Resize(ref arr, arr.Length - 1);
}
Removing from an array itself is not simple, as you then have to deal with resizing. This is one of the great advantages of using something like a List<int> instead. It provides Remove/RemoveAt in 2.0, and lots of LINQ extensions for 3.0.
If you can, refactor to use a List<> or similar.
Balabaster's answer is correct if you want to remove all instances of the element. If you want to remove only the first one, you would do something like this:
int[] numbers = { 1, 3, 4, 9, 2, 4 };
int numToRemove = 4;
int firstFoundIndex = Array.IndexOf(numbers, numToRemove);
if (numbers >= 0)
{
numbers = numbers.Take(firstFoundIndex).Concat(numbers.Skip(firstFoundIndex + 1)).ToArray();
}
As a generic extension, 2.0-compatible:
using System.Collections.Generic;
public static class Extensions {
//=========================================================================
// Removes all instances of [itemToRemove] from array [original]
// Returns the new array, without modifying [original] directly
// .Net2.0-compatible
public static T[] RemoveFromArray<T> (this T[] original, T itemToRemove) {
int numIdx = System.Array.IndexOf(original, itemToRemove);
if (numIdx == -1) return original;
List<T> tmp = new List<T>(original);
tmp.RemoveAt(numIdx);
return tmp.ToArray();
}
}
Usage:
int[] numbers = {1, 3, 4, 9, 2};
numbers = numbers.RemoveFromArray(4);
You can do in this way:
int[] numbers= {1,3,4,9,2};
List<int> lst_numbers = new List<int>(numbers);
int required_number = 4;
int i = 0;
foreach (int number in lst_numbers)
{
if(number == required_number)
{
break;
}
i++;
}
lst_numbers.RemoveAt(i);
numbers = lst_numbers.ToArray();
' To remove items from string based on Dictionary key values.
' VB.net code
Dim stringArr As String() = "file1,file2,file3,file4,file5,file6".Split(","c)
Dim test As Dictionary(Of String, String) = New Dictionary(Of String, String)
test.Add("file3", "description")
test.Add("file5", "description")
stringArr = stringArr.Except(test.Keys).ToArray()
public int[] DeletePart(int position, params int[] numbers)
{
int[] result = new int[numbers.Length - 1];
int z=0;
for (int i = 0; i < numbers.Length; i++)
{
if (position - 1 != i)
{
result[z] = numbers[i];
z++;
}
}
return result;
}
We can delete array elements by using for loops and continue statements:
string[] cars = {"volvo", "benz", "ford", "bmw"};
for (int i = 0; i < cars.Length; i++)
{
if (cars[i] == "benz")
{
continue;
}
Console.WriteLine(cars[i]);
}

Categories

Resources