-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumofalloddfreqelements.cs
94 lines (78 loc) · 1.56 KB
/
sumofalloddfreqelements.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int[] ele = new int[7] {1, 1, 2, 2, 3, 3, 3};
addoddfreqelements(ele);
}
public static void addoddfreqelements(int[] arr)
{
int length = arr.Length;
Dictionary<int, int> map = new Dictionary<int, int>();
for(int i =0; i < length; i++)
{
if(map.ContainsKey(arr[i]))
{
int count = map[arr[i]];
map[arr[i]] = count + 1;
}
else
{
map.Add(arr[i], 1);
}
}
int sum = 0;
foreach(var kvp in map)
{
if(kvp.Value % 2 !=0)
{
sum = kvp.Key * kvp.Value;
Console.WriteLine(sum + " ");
}
}
}
public static int countdistinct(int[] arr)
{
int length = arr.Length;
HashSet<int> distinct = new HashSet<int>();
for(int i =0; i< length; i++)
{
distinct.Add(arr[i]);
}
Console.Write(distinct.Count);
return distinct.Count;
}
public static int MaxDifference(int[] arr)
{
int length = arr.Length;
int max_diff = 0;
int min = arr[0];
int max = arr[length-1];
for (int i = 1; i < length; i++)
{
//larger element appears after smaller elem check, so diff is always greater than equal to zero
if (arr[i] - min > max_diff)
{
max_diff = arr[i] - min;
if (arr[i] < min)
{
min = arr[i];
}
if (arr[i] > max)
{
max = arr[i];
}
}
if(arr[i] < min) //update min if necessary
{
min = arr[i];
}
}
Console.WriteLine("min:" + min);
Console.WriteLine("max:" + max);
//Console.WriteLine(max_diff);
return max_diff;
}
}