0% found this document useful (0 votes)
4 views3 pages

C# Array Question

The document provides two C# methods for handling array elements. The first method counts the occurrences of each element in an integer array and displays the frequency, while the second method checks for duplicates in an array using a dictionary. If any value appears more than once, it returns true; otherwise, it returns false.

Uploaded by

Jagdish Tripathi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

C# Array Question

The document provides two C# methods for handling array elements. The first method counts the occurrences of each element in an integer array and displays the frequency, while the second method checks for duplicates in an array using a dictionary. If any value appears more than once, it returns true; otherwise, it returns false.

Uploaded by

Jagdish Tripathi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Occurrence Of Array Element in C#

public static void OccurenceOfArrayElement()


{
int[] arr = new int[] { 1, 4, 2, 5, 4, 2, 5, 1, 6, 2, 5, 3, 8, 9,
6 };

int[] occurence = new int[[Link]];


int visit = -1;
for (int i = 0; i < [Link]; i++)
{
int count = 1;
for (int j = i + 1; j < [Link]; j++)
{
if (arr[i] == arr[j])
{
count++;
//To avoid counting same element again
occurence[j] = visit;
}
}
if (occurence[i] != visit)
occurence[i] = count;
}
//Displays the frequency of each element present in array
for (int i = 0; i < [Link]; i++)
{
if (occurence[i] != visit)
[Link]("element- " + arr[i] + " occurence is " +
occurence[i]);
}
[Link]();
}
Output:-
//Given an array of integers, find if the array contains any
duplicates.
//Your function should return true if any value appears at
least twice in the array, and it should return false if every
element is distinct.
public static class ArrayDuplicates
{
//Dictionary solution
public static bool ContainsDuplicates(params int[] x)
{
Dictionary<int, int> d = new Dictionary<int, int>();
foreach (int i in x)
{
if ([Link](i))
return true;
else
[Link](i, 1);
}
return false;
}
}
//Given an array of integers, find if the array contains any
duplicates.
//Your function should return true if any value appears at
least twice in the array, and it should return false if every
element is distinct.
public static class ArrayDuplicates
{
//Dictionary solution
public static bool ContainsDuplicates(params int[] x)
{
Dictionary<int, int> d = new Dictionary<int, int>();
foreach (int i in x)
{
if ([Link](i))
return true;
else
[Link](i, 1);
}
return false;
}
}

You might also like