Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if my array has repeated values inside it?

Tags:

So here is my array.

double[] testArray = new double[10]; // will generate a random numbers from 1-20, too lazy to write the code 

I want to make a search loop to check if any values are being repeated. How do I do that?

I would prefer not to use any special built-in methods since this is a small array.

like image 987
puretppc Avatar asked Nov 03 '13 20:11

puretppc


People also ask

How do you check if an array contains repeated values?

function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = i+1; j < myArray. length; j++) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } return false; // means there are no duplicate values. }

How do you check if there are duplicates in an array in C?

To count total duplicate elements in given array we need two loops. Run an outer loop loop from 0 to size . Loop structure must look like for(i=0; i<size; i++) . This loop is used to select each element of array and check next subsequent elements for duplicates elements using another nested loop.

How do you find duplicates in a set array?

The standard way to find duplicate elements from an array is by using the HashSet data structure. If you remember, Set abstract data type doesn't allow duplicates. You can take advantage of this property to filter duplicate elements.

How do I check if a string is repeated in an array?

Step to find duplicate in String[] Array :Get length of String Arrays using length property of Arrays. Similarly get size of Set/HashSet object using size() method. Finally compare Arrays length with Set size using if-else statement.


2 Answers

You could do this with a little Linq:

if (testArray.Length != testArray.Distinct().Count()) {     Console.WriteLine("Contains duplicates"); } 

The Distinct extension method removes any duplicates, and Count gets the size of the result set. If they differ at all, then there are some duplicates in the list.

Alternatively, here's more complicated query, but it may be a bit more efficient:

if (testArray.GroupBy(x => x).Any(g => g.Count() > 1)) {     Console.WriteLine("Contains duplicates"); } 

The GroupBy method will group any identical elements together, and Any return true if any of the groups has more than one element.

Both of the above solutions work by utilizing a HashSet<T>, but you can use one directly like this:

if (!testArray.All(new HashSet<double>().Add)) {     Console.WriteLine("Contains duplicates"); } 

Or if you prefer a solution that doesn't rely on Linq at all:

var hashSet = new HashSet<double>(); foreach(var x in testArray)  {     if (!hashSet.Add(x))      {         Console.WriteLine("Contains duplicates");         break;     } } 
like image 194
p.s.w.g Avatar answered Oct 19 '22 23:10

p.s.w.g


take look at my implementation its generic and efficient

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     } 

here are some calls

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 
like image 39
Basheer AL-MOMANI Avatar answered Oct 19 '22 22:10

Basheer AL-MOMANI