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;
}
}