Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Check if 2 int arrays contain only unique numbers at the same index

If I have 2 int arrays a and b and they contain data like this..

a[0] = 1
a[1] = 3
a[2] = 7

b[0] = 6
b[1] = 3
b[2] = 5

How can I check if all the pairs of numbers are unique e.g. that each combination of a[i] and b[i] at the same index is not repeated in the rest of the array... So the above data would pass but if I introduced this below it would fail..

a[24] = 7
b[24] = 5

Because this combination already exists in the array at index 2. Can I do this in LINQ?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

try this sample :

        int [] aa = a.Distinct().ToArray(); 

or :

public static bool HasDuplicates<T>(IList<T> items)
    {
        Dictionary<T, bool> map = new Dictionary<T, bool>();
        for (int i = 0; i < items.Count; i++)
        {
            if (map.ContainsKey(items[i]))
            {
                return true; // has duplicates
            }
            map.Add(items[i], true);
        }
        return false; // no duplicates
    }

and call :

string[] strings = new[] { "1", "2", "3" };
Utility.HasDuplicates(strings)// this will return false

int[] items=new []{1,2,3,1};
Utility.HasDuplicates(items)// this will return true
over 4 years ago · Santiago Trujillo Report

0

If the order of the values in the pairs over the two arrays is significant (i.e. a[0] == 1, b[0] == 2 is considered different from a[0] == 2, b[0] == 1) then one way to check for uniqueness using Linq is as follows:

bool unique = a.Zip(b).Distinct().Count() == a.Length;

If the order of the values in the pairs is NOT significant, it's slightly more fiddly:

bool unique = a.Zip(b).DistinctBy(
     x => (Math.Min(x.First, x.Second), Math.Max(x.First,x.Second)))
.Count() == a.Length;

These solutions assume that missing values in one of the arrays will be ignored.

(Note: DistinctBy() is only available in .Net 6.0 or later, or via a NuGet package.)

over 4 years ago · Santiago Trujillo Report

0

I've managed to work this out and solve this with :

a.Zip(b, (aPos, bPos) => new { aPosition = aPos, bPosition = bPos }).Distinct().Count()

This will tell me how many distinct sets of both values at the same index and so I can work the rest out from here.

Apologies if my question wasn't clear.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!