Find a number without a pair in an array

I am having problems with a small amount of code that is in an array of arbitrary size with random number pairs, except for one that does not have a pair.

I need to find this number that does not have a pair.

arLength is the length of the array. but I am having problems with the actual match of the pairs and the search for one that does not have a pair.

 for (int i = 0; i <= arLength; i++)
        { // go through the array one by one..
            var number = nArray[i];

            // now search through the array for a match.
            for (int e = 0; e <= arLength; e++)
            {
                if (e != i)
                {

                }
            }
        }

I also tried this:

var findValue = nArray.Distinct();

I searched around, but so far I have not been able to find a way to do this.

This code generates an array, but this question does not concern this part of the code, just for clarity.

Random num = new Random();
            int check = CheckIfOdd(num.Next(1, 1000000));
            int counter = 1;

            while (check <= 0)
            {
                if (check % 2 == 0)
                {
                    check = CheckIfOdd(num.Next(1, 1000000)); ;
                }
                counter++;
            }
            int[] nArray = new int[check];
            int arLength = 0;
            //generate arrays with pairs of numbers, and one number which does not pair.
            for (int i = 0; i < check; i++)
            {
                arLength = nArray.Length;

                if (arLength == i + 1) 
                {
                    nArray[i] = i + 1;
                }
                else
                {
                    nArray[i] = i;
                    nArray[i + 1] = i;
                }
                i++;
            }
+3
source share
6 answers

Distictwill provide you an array with various values. he will not find the desired value.

GroupBy Count modulo 2 1.

var noPairs = nArray.GroupBy(i => i)
                    .Where(g => g.Count() % 2 == 1)
                    .Select(g=> g.Key);
+4

bitwise ^, O(n).

operator ^ aka xor :

enter image description here

, , , , .

var element = nArray[0];

for(int i = 1; i < arLength; i++) 
{
    element = element ^ nArray[i];
}

, element .

+3

. , . , .

. , .

    for (int i = 0; i <= arLength; i++)
    {
        bool hasMatch = false;
        for (int e = 0; e <= arLength; e++)
        {
            if (nArray[e] == nArray[i])//Compare the element, not the index.
            {
               hasMatch = true;
            }
        }
        //if hasMatch == false, you found your item.
    }
0

. , () 2.

using System.Linq;

int[] data = new[] {1, 2, 3, 4, 5, 3, 2, 4, 1};

// key is the number, value is its count
var numberCounts = new Dictionary<int, int>();

foreach (var number in data) {
    if (numberCounts.ContainsKey(number)) {
        numberCounts[number]++;
    }
    else {
        numberCounts.Add(number, 1);
    }
}

var noPair = numberCounts.Single(kvp => kvp.Value < 2);
Console.WriteLine(noPair.Key);

- O (n), , . ..

.NET Fiddle

0

, , Xor :

 int result = nArray.Aggregate((s, a) => s ^ a);

, , : a ^ a == 0 distinc: 0 ^ 0 ^ ...^ 0 ^ distinct ^ 0 ^ ... ^0 == distinct

0

, , , ?

var total = new Random().Next(500000) * 2 + 1;
var myArray = new int[total];
for (var i = 1; i < total; i+=2)
{
    myArray[i] = i;
    myArray[i -1] = i;
}
myArray[total - 1] = total;

Linq, , . , :

var key = myArray.GroupBy(t => t).FirstOrDefault(g=>g.Count()==1)?.Key;
0

All Articles