Reputation: 1023
The question is there is and unsorted array and the maximum value should be smaller than the length. I have to find the duplicate record in the array. The condition is to use a loop only once. This is what i have achieved so far. I wanted to know if there was any other approach through which i can achieve this.
int[] Arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };
int[] Arr2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int i = 0; i < Arr.Length; i++)
{
if (Arr2[Arr[i]] == 0)
{
Arr2[Arr[i]] = Arr[i];
}
else
{
Console.WriteLine("duclicate found");
}
}
Upvotes: 6
Views: 21903
Reputation: 186668
Use any Set
implementation, say HashSet<T>
, e.g.
HashSet<int> hs = new HashSet<int>();
int[] Arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };
foreach (item in Arr)
if (hs.Contains(item)) {
Console.WriteLine("duplicate found");
// break; // <- uncomment this if you want one message only
}
else
hs.Add(item);
Edit: since hs.Add
returns bool
a shorter and more efficient code can be put:
HashSet<int> hs = new HashSet<int>();
int[] Arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };
foreach (item in Arr)
if (!hs.Add(item)) {
Console.WriteLine("duplicate found");
// break; // <- uncomment this if you want one message only
}
Upvotes: 10
Reputation: 2520
Since you have this condition :
The question is there is and unsorted array and the maximum value should be smaller than the length.
Also assuming only positive
numbers, which in your example applies
This can be done using O(n)
time and O(1)
space without using any LINQ, Dictionary, Hashing etc.
int[] arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };
for (int i = 0; i < arr.Length; i++)
{
if (arr[Math.Abs(arr[i])] >= 0)
arr[Math.Abs(arr[i])] = -arr[Math.Abs(arr[i])];
else
Console.WriteLine("Duplicate found " + Math.Abs(arr[i]).ToString() + "\n");
}
Upvotes: 3
Reputation: 914
try this code using LINQ
int[] listOfItems = new[] { 4, 2, 3, 1, 6, 4, 3 };
var duplicates = listOfItems
.GroupBy(i => i)
.Where(g => g.Count() > 1)
.Select(g => g.Key);
foreach (var d in duplicates)
Console.WriteLine("The duplicate is "+d);
Upvotes: 0
Reputation: 2723
This will work if only array a[]
contains numbers in range [0,n-1]
{as in your question}
and n is not very large to avoid integer range overflow .
for(i=0;i<n;i++)
{
if(a[a[i]%n]>=n)
**duplicate is a[i]** !
else
a[a[i]%n]+=n;
}
Time complexity : O(N)
Space complexity : O(1) !
Upvotes: 0
Reputation: 3833
The fastest way to obtain all duplicates, using LINQ, is this:
var duplicates = Arr.GroupBy(s => s).SelectMany(d => d.Skip(1));
This will return an IEnumerable
of all duplicate elements in Arr
, and you can use the following check to contain if there were any duplicates:
if (duplicates.Any())
{
// We have a duplicate!
}
Upvotes: 0
Reputation: 178431
This is the Element Distinctness Problem.
This problem cannot be solved strictly linearly without additional space.
The two common approaches to solve the problem are:
O(nlogn)
time and very little extra space.Upvotes: 2