using System namespace lab22 class Program private static int arr stat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
namespace lab22
{
class Program
{
private static int[] arr;
static void Main()
{
arr = new int[] {1,2,3,4,5,6,7,8,9,1,3,4,6,2,7,8};
Console.WriteLine("Исходный массив:");
foreach (int item in arr)
Console.Write(item + " ");
var difEl = new DifferentElements(arr);
Console.WriteLine ("\n\nКоличество различных элементов в массиве = " + difEl.GetCount());
Console.ReadKey();
}
}
class DifferentElements
{
private int[] arr;
private int countDiffVal;
public DifferentElements(int[] arr)
{
this.arr = arr;
}
public int GetCount()
{
int len = arr.Length;
int cnt = 0;
for(int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
if (arr[i] == arr[j])
cnt++;
if (cnt < 2)
countDiffVal++;
cnt = 0;
}
return countDiffVal;
}
}
}