-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCollections_Log.cs
More file actions
104 lines (95 loc) · 3.21 KB
/
Collections_Log.cs
File metadata and controls
104 lines (95 loc) · 3.21 KB
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
95
96
97
98
99
100
101
102
103
104
/*
*Author: Vishva Patel
* Program: TCS C# hands-on Collections
* Github: .NET-Console-Apps
*/
using System;
using System.Collections;
namespace Contestantlog
{
class Collections_Log
{
static void Main(string[] args)
{
int n;
n = Convert.ToInt32(Console.ReadLine());
ArrayList names = new ArrayList(n); //This will store the contestant log, instead of array list one can use string array
//as well but this collection comes with predefined extensions.
int[] count = new int[n]; //This stores the count of the completed task by each contestant.
string value; //variable to store the contestant log.
int j, max = 0; //j is the link between both arrays, max is to find out the number of task completed by the winner(s).
//This loop takes input and also performs the necessary operations accordingly.
for (int i = 0; i < n; i++)
{
value = Console.ReadLine();
//we need to first add something before iterating or checking the name.
if (i > 0)
{
if (names.Contains(value)) //if name is already in the list then no need to add again only increment the count.
{
j = names.IndexOf(value); //Finding the index of the name, this is responsiblefor keeping the list in sync.
count[j]++; //incrementing the count.
}
//What happens if the new name is added to log.
else
{
names.Add(value); //first add the name to the list
j = names.IndexOf(value); //Find the index to which it is added which will by default be the next index to previous names index.
count[j] = 1; //setting the count as 1 as it has completed one task.
}
}
//This block adds the first name.
else
{
names.Add(value);
j = names.IndexOf(value);
count[j] = 1;
}
}
//There can be multiple winners as well with same number of tasks completed.
foreach (int c in count)
{
if (c >= max)
{
max = c; //getting the maximum value of the task completed.
}
}
//printing the names of contestants who have completed the max tasks and are winners.
for (int i = 0; i < count.Length; i++)
{
if (count[i] == max)
{
Console.WriteLine(names[i]);
}
}
}
}
}
//Sample outputs:
/* Test Case 1
4
john
harry
john
harry
-----o/p
john
harry
Test case 2
7
ram
rajesh
ravi
kunal
pravina
uma
hari
------o/p
ram
rajesh
ravi
kunal
pravina
uma
hari
*/