0

I would like to create a for loop to check if an index value has repeated, and if it has to remove it from the original and display an updated list with no repeated index values.

I have and Array (called Original) with multiple values being repeated, and then I created a Temporary Array (called TempArray) with the same exact values.

I want to check the Original and TempArray values against each other to see if there are duplicates.

I want my code to be similar to a previous one I did where I deleted an index from the middle of my array, as follows

for (int i = 0; i < Original.Length; i++)
{
    TempArray[i] = Original[i];
}

Original = new int[Original.Length - 1];

//Set index to delete
int DeleteIndex = 3;  

//modify size of Orignial array and copy in array without deleted element index
for (int i = 0; i < TempArray.Length; i++)
{
    if (i < DeleteIndex)
    {
        Original[i] = TempArray[i];
    }

    else if (i > DeleteIndex)
    {
        Original[i - 1] = TempArray[i];
    }
}

P.S. Original[] is an array from a text file. Sorry if my explanation is hard to understand as I am new to computer programming.

Also is there any way to do this code without LINQ, I have to create using arrays only as its all thats been taught at school?

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Cool Cat
  • 127
  • 2
  • 9
  • Why don't you create a new array and insert there the values of `Original` as long as they are not already there? `if (!NewArray.Contains(Original[i])) NewArray[j] = Original[i];` Note that you need two separate indexes. – Andrew Sep 28 '15 at 03:37
  • Use a List<> instead of an array with the built-in function to easily perform deletes, and inserts into an array. – jdweng Sep 28 '15 at 03:37
  • All your novel can be reduced to how to remove index from array. And with search tool called google you will end up here http://stackoverflow.com/questions/457453/remove-element-of-a-regular-array – M.kazem Akhgary Sep 28 '15 at 03:38
  • possible duplicate of [Remove duplicates from array](http://stackoverflow.com/questions/9673/remove-duplicates-from-array) – M.kazem Akhgary Sep 28 '15 at 03:42

2 Answers2

3

It took some time to understand what you want :)

  1. to check if an index value has repeated, and if it has to remove it from the original and display an updated list with no repeated index values

    It is shortly called distinct values. There is a LINQ method .Distinct():

    Original = Original.Distinct().ToArray();
    

    It will remove all repeated values.

  2. where I deleted an index from the middle of my array

    You can do this easier by converting to List, applying RemoveAt and back:

    var list = new List<int>(Original);
    list.RemoveAt(DeleteIndex);
    Original = list.ToArray();
    

P.S. Assuming that you say "index" about the values in your array, it is pretty confusing. Array does not store indices - index is usually understood as a key of an array.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
1

May be this will do the trick for you. I am assuming that you need to remove duplicate from your array

Original = Original.Distinct().ToArray();

Will Returns distinct elements from a sequence by using the default equality comparer to compare values.

Mohit S
  • 13,723
  • 6
  • 34
  • 69