KacaMat
KacaMat

Reputation: 49

Duplicated elements from x array add into y array

I tried to find 2 or more same elements from array x and then that duplicate to add into new array Y

So if i have in x array number like: 2,5,7,2,8 I want to add numbers 2 into y array

int[] x = new int[20];
        Random rnd = new Random();
        int[] y = new int[20];
        int counter = 0;


        for (int i = 0; i < x.Length; i++)
        {
            x[i] = rnd.Next(1, 15);

            for (int j=i+1;  j< x.Length; j++)
            {
                if (x[i] == x[j]) 
                {

                    y[counter] = x[i];
                    Console.WriteLine("Repeated numbers are " + y[counter]);
                    counter++;
                }
                else
                {
                    Console.WriteLine("There is no repeated numbers, numbers that are in x are  " + x[i]);
                }
                break;
            }
        }

But having problems with that, when it come to the if loop it doesn't want to proceed with executing if loop (even if condition is true)

If someone could give me some suggestion, that would be helpful, thank you

Upvotes: 1

Views: 126

Answers (6)

Wannes Geboers
Wannes Geboers

Reputation: 36

at first: why do u use the 'break;' ?

second: in the first for - loop u assign a random number to x[i] but then in the nested second loop u already ask x[j] to check for same values (but that doesn't exist yet)

there are so many ways to check if values are equal, but i like your approach: so what i would suggest:

make a for - loop and assign all the random numbers to int[] x

then think again how u can evaluate x[0] = x[1] or x[2] or x[3] ...

Upvotes: 2

xanatos
xanatos

Reputation: 111840

There are various logical errors in your use of for. You should work more on your logic, because while libraries can be learnt by rote, logical errors are more something that is inside you.

int[] x = new int[20];
Random rnd = new Random(5);

// You don't know the length of y! 
// So you can't use arrays
List<int> y = new List<int>();

// First initialize
for (int i = 0; i < x.Length; i++)
{
    x[i] = rnd.Next(1, 15);
}

// Then print the generated numbers, otherwise you won't know what numbers are there
Console.WriteLine("Numbers that are in x are: ");
for (int i = 0; i < x.Length; i++)
{
    Console.WriteLine(x[i]);
}

// A blank line
Console.WriteLine();

// Then scan
for (int i = 0; i < x.Length; i++)
{
    for (int j = i + 1; j < x.Length; j++)
    {
        if (x[i] == x[j])
        {
            y.Add(x[i]);
            Console.WriteLine("Repeated numbers is " + x[i]);
        }
    }
}

// Success/failure in finding repeated numbers can be decided only at the end of the scan
if (y.Count == 0)
{
    Console.WriteLine("There is no repeated numbers");
}

I've put some comments in the code (plus the changes)

And for debugging purpose, I suggest you use a fixed Random sequence. new Random(5) (or any other number) will return the same sequence every time you launch your program.

Note that if there are multiple repetitions of a number, like { 4, 4, 4 } then the y array will be { 4, 4 }

Upvotes: 4

Michał Turczyn
Michał Turczyn

Reputation: 37347

I think this will will be the most understandable solution without complicated extension methods:

int[] x = new int[20];
// there can be at most 10 duplicates in array of length of 20 :)
// you could use List<int> to easily add elements
int[] y = new int[10];
int counter = 0;
Random rnd = new Random();
// fill the array
for (int i = 0; i < x.Length; i++)
    x[i] = rnd.Next(1, 15);
// iterate through distinct elements,
// otherwise, we would add multiple times duplicates
foreach (int i in x.Distinct())
    // if the count of an elements is greater than one, then we have duplicate
    if(x.Count(n => n == i) > 1)
    {
        y[counter] = i;
        counter++;
    }

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176896

Make use of linq in your code , as below

//first populate array x 
var duplicates= xArray.GroupBy(x => x)
              .Where(g => g.Count() > 1)
              .Select(y => y.Key)
              .ToArray();

linq query above make use of groupby and find duplicate i.e. element occuring more then one time in you array and then you select those element and return result

Upvotes: 0

Amit
Amit

Reputation: 1857

int[] array = new int[5] {1,2,3,4,4};
List<int> duplitcateList = array.Where(x => array.Where(y => y == x).Count() > 1).Distinct().ToList();

or you can replace last line of above code with below.

List<int> duplitcateList = array.
    GroupBy(x => x).Where(g => g.Count() > 1).Select(g => g.Key).ToList();

above code is using Linq.

suppose your first array (in question x) is array. Linq will first check for all elements in to list which occur more then once, and select them distinctly and store it to duplicateList

if you need an array at the, you can simply convert this list to array by doing this,

int[] yArray = duplitcateList.ToArray();

Upvotes: 0

Herman
Herman

Reputation: 2978

Try to use Linq to find the duplicate in the Array

int[] x = new int[] { 2, 5, 7, 2, 8 };
int[] y;

var result = x.GroupBy(item => item)
              .Select(grp => new { key = grp.Key, Count = grp.Count() });

y = result.Where(res => res.Count > 1).Select(res => res.key).ToArray();

Upvotes: 0

Related Questions