Cannot find duplicates in array [duplicate] - c#

This question already has answers here:
How do I check if my array has repeated values inside it?
(8 answers)
Find duplicates between arrays
(16 answers)
Closed 8 years ago.
I need to find duplicate numbers in this method. Ive tried this, but i think im missing something. anyone can see what I'm doing wrong.
public bool FindDublicate(int[] a)
{
bool svar = true;
for (int i = 0; i < a.Length; i++)
{
for (int j = i; j < a.Length - 1; j++)
{
if (a[j] == a[j + 1])
{
svar = true;
}
else return false;
}
}
return svar;
}

If you are only interested in knowing whether your array contains duplicate or not you can do:
return a.Distinct().Count() != a.Length;

Related

MergeSort Algorithm "IndexOutOfRangeException" [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 1 year ago.
I've been reading the Introduction to Algorithms and tried to implement the pseudocode, but I can't seem to pinpoint what is causing IndexOutOfRangeException.
Merge
The error happens at left[i] < right[j]
static void Merge(int[] array, int start, int middle, int end)
{
i = 1;
j = 1;
for (k = start; k < end; k++)
{
if (left[i] < right[j])
{
array[k] = left[i];
i = i + 1; // -> i++;
}
else
{
array[k] = right[j];
j = j + 1; // -> j++;
}
}
}
Solved
I solved it by setting all i and j variables to 0 and k to k = start - 1
You are getting error in this line:
if (left[i] < right[j])
since i exceeds left array length when it becomes 6 and array left has only 6 elements.
It is very easy to figure it out when you use debugging.

How to swap 2 values in a List in C#? [duplicate]

This question already has answers here:
Swap two items in List<T>
(6 answers)
Closed 2 years ago.
So iam new to c# and trying to making a bubble sort algorithm and was wondering is there a way to swap 2 values in a list in the simplest way?
static void BubbleSort(List<int> list)
{
for (int i = 0; i == list.Count-1; i++)
{
for (int j = 0; i == list.Count - i; j++)
{
if (list[j] > list[j+1])
{
//Need a way to swap values in position j and j+1
}
}
}
Keep it simple, use a temporary variable to momentanly store one of two values:
if (list[j] > list[j+1])
{
int temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
if (list[j] > list[j + 1])
(list[j], list[j + 1]) = (list[j + 1], list[j]);

Adding array to a list how to stop it being the last iteration data? [duplicate]

This question already has answers here:
C# dictionary value clearing out when I clear list previously assigned to it....why?
(6 answers)
Closed 6 years ago.
I am trying to create a list of an array of 2 items. The problem I have is the final iteration in my loop sets all the list's arrays to the same (my guess is due to a reference?). I was wondering how I can prevent this from happening.
This is what I am trying to do:
PathTiles[] links = new PathTiles[2];
int j;
for(int i = 0; i < path.Count; i++)
{
if(i+1 < path.Count)
{
links[0] = path[i];
links[1] = path[i];
for(j = i+1; j < path.Count; j++)
{
if(path[j].x == links[1].x && Mathf.Abs(path[j].y-links[1].y) == 1)
{
links[1] = path[j];
i = j;
}
else
{
break;
}
}
validPath.Add(links);
for(int k = 0; k < validPath.Count;k++)
{
Debug.Log(validPath[k].x+", "+validPath[k].y);
}
Debug.Log("=====");
}
}
So the console shows this:
0, 0
=====
1, 1 <-- why did this become 1, 1 and not stay as 0, 0
1, 1
=====
Does any one know how I can retain the first data value and not have it overwritten? I am currently under the impression it is because it stores a reference and so I changed the values, but I don't know how to stop it doing that.
Try adding the PathTiles[] links = new PathTiles[2]; within the 1st for loop statement. As you said this is a problem of references. As you were addiing the same modified links to the list. the 1st data value is overwritten.
int j;
for (int i = 0; i < path.Count; i++)
{
PathTiles[] links = new PathTiles[2]; //<-- here
if (i + 1 < path.Count)
{
links[0] = path[i];
links[1] = path[i];
...

Index was out of range [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 7 years ago.
Can somebody help with this snippet of code which has an exception. It always stacks on [2,0] index. I just can't figure out how to solve this. I have added a screenshot below.
private Matrix GridToMatrix(DataGridView grid)
{
var matrix = new double[grid.RowCount, grid.ColumnCount];
for (int i = 0; i < grid.RowCount; i++)
{
for (int j = 0; j < grid.ColumnCount; j++)
{
matrix[i, j] = Convert.ToSingle(grid[i, j].Value);
}
}
return new Matrix(matrix);
}
The problem here is the confusing indexing mechanism of DataGridView.
The indexing for DataGridView is like [col, row] while Matrix indexing is [row, col]
By changing grid[i, j] to grid[j, i] the problem will resolve.

list.RemoveAt in loop [duplicate]

This question already has answers here:
How to remove elements from a generic list while iterating over it?
(28 answers)
Closed 8 years ago.
for (int j = 0; j < list.Count; ++j )
{
if (list[j].current_status == false)
{
list.RemoveAt(j);
}
}
I want to remove all the items which has current_status == false in the list, However,
When execute the statement: list.RemoveAt(j), the list.Count will minus one. Finally, the original loop can not be looped from list[0] to list[list.Count]. How can I deal with this situation?
Thanks
You could use RemoveAll instead of RemoveAt:
list.RemoveAll(item => item.current_status == false);
int size = list.Count;
for (int j = 0; j < size; ++j )
{
if (list[j].current_status == false)
{
list.RemoveAt(j);
size--;
j--;
}
}
Also running linear time, a reverse loop like so:
for (int j = length - 1; j >= 0; j--)
{
if (!list[j].current_status)
{
list.RemoveAt(j);
}
}
Side note : try not to test booleans with "==" and/or "!=" but use the expression itself or the negation symbol is needed

Categories