Counting times values occured in Array - c#

I am having a hard time with a program I need to write. The requirements are that the user enters in the size of the Array. Then they enter the elements of the array after this the program needs to display the values entered then how many times each value occurs. While everything seems to work except its not display the "occurs" part properly. Lets say "1 1 2 3 4" is entered (Array size being 5) It prints
1 occurs 1 time.
1 occurs 1 time.
2 occurs 1 time.
3 occurs 1 time.
4 occurs 2 times.
this isn't right because 1 occured 2 times and 4 is only 1 time. Please Help...
static void Main(string[] args)
{
int[] arr = new int[30];
int size,
count=0,
count1=0,
count2=0;
Console.Write("Enter Size of the Array: ");
size = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the elements of an Array: ");
for (int i=0; i < size; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Values Entered: \n");
for (int i = 0; i < size; i++)
{
Console.WriteLine(arr[i]);
if (arr[i] <= 10)
count++;
else
count1++;
}
for(int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (arr[i] == arr[j])
count2++;
else
count2 = 1;
}
Console.WriteLine(arr[i] + " Occurs " + count2 + " Times.");
}
Console.WriteLine("The number of valid entries are: " + count + "\nThe number of invalid entries are: " + count1);
Console.ReadKey();
}

if you can use Linq, this is easy job:
After
Console.Write("Values Entered: \n");
add
var grouped = arr.GroupBy(x => x);
foreach (var group in grouped)
{
Console.WriteLine("number {0} occurs {1} times", group.Key, group.Count());
}
EDIT
Since OP isn't allowed to use Linq, here's array-only solution. Much more code than that dictionary approach, but with arrays only.
Console.Write("Values Entered: \n");
//an array to hold numbers that are already processed/counted. Inital length is as same as original array's
int[] doneNumbers = new int[arr.Length];
//counter for processed numbers
int doneCount = 0;
//first loop
foreach (var element in arr)
{
//flag to skip already processed number
bool skip = false;
//check if current number is already in "done" array
foreach (int i in doneNumbers)
{
//it is!
if (i == element)
{
//set skip flag
skip = true;
break;
}
}
//this number is already processed, exit loop to go to next one
if (skip)
continue;
//it hasn't been processed yes, so go through another loop to count occurrences
int occursCounter = 0;
foreach (var element2 in arr)
{
if (element2 == element)
occursCounter++;
}
//number is processed, add it to "done" list and increase "done" counter
doneNumbers[doneCount] = element;
doneCount++;
Console.WriteLine("number {0} occurs {1} times", element, occursCounter);
}

You can simply use dictionary:
static void Main(string[] args)
{
var dic = new Dictionary<int, int>();
Console.Write("Enter Size of the Array: ");
int size = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the elements of an Array: ");
for (int i = 0; i < size; i++)
{
int val = Convert.ToInt32(Console.ReadLine());
int current;
if (dic.TryGetValue(i, out current))
{
dic[val] = current + 1;
}
else
{
dic.Add(val, 1);
}
}
foreach (int key in dic.Keys)
{
Console.WriteLine(key + " Occurs " + dic[key] + " Times.");
}
Console.Read();
}

The problem is that you're resetting count2 to 1 any time you find a mismatch.
What you should do instead is set count2 to 0 in the outer loop (so it basically resets once for each item), and then let the inner loop count all the instances of that number:
// For each item 'i' in the array, count how many other items 'j' are the same
for (int i = 0; i < size; i++)
{
count2 = 0; // processing a new item, so reset count2 to 0
for (int j = 0; j < size; j++)
{
if (arr[i] == arr[j]) count2++;
}
Console.WriteLine(arr[i] + " occurs " + count2 + " times.");
}

Related

C#: how to detect repeating values in an array and process them in such a way that each repeating value is only processed once?

I wrote this simple program:
class Program
{
static void Main(string[] args)
{
Console.Write("Number of elements in the array: ");
int numberOfElements = int.Parse(Console.ReadLine());
int[] array = new int[numberOfElements];
for(int i = 0; i < numberOfElements; i++)
{
Console.Write($"Element no {i+1}: ");
array[i] = int.Parse(Console.ReadLine());
}
for(int i = 0; i < array.Length; i++)
{
int count = 0;
for(int j = 0; j < array.Length; j++)
{
if(array[i] == array[j])
{
count++;
}
}
Console.WriteLine($"{array[i]} appears " + count + " times");
}
}
}
}
Is there any option to make the displayed values print only once?
For example, if there are three occurrences - the message displays three times. Is it possible to make it display once when there are more occurrences though?
You could use a GroupBy instead of the for loop
Groups the elements of a sequence.
var results = array
.GroupBy(x => x)
.Select(x => new {Value = x, Count = x.Count()});
foreach(var g in results)
Console.WriteLine($"{g.Value} appears {g.Count} times");
Or another way it to use a HashSet to keep track of what you have displayed. A HashSet is basically a collection that contains no duplicate elements. The Add methods returns true if it can add an element or false otherwise
HashSet<T>.Add(T) Method
returns true if the element is added to the HashSet object; false if the
element is already present.
var hashSet = new HashSet<int>();
for (int i = 0; i < array.Length; i++)
{
int count = 0;
for (int j = 0; j < array.Length; j++)
if (array[i] == array[j])
count++;
// Add to the hashset, if the add method returns true,
// it means the value was uniquely added, ergo you have not displayed yet
if (hashSet.Add(array[i]))
Console.WriteLine($"{array[i]} appears " + count + " times");
}
Full Demo Here
Or another approach is to use a Dictionary. The premise is to iterate over the array, try an add each item to the dictionary with TryAdd if it's already found increment the value
var dictionary = new Dictionary<int,int>();
foreach(var item in array)
if(!dictionary.TryAdd(item,1))
dictionary[item]++;
foreach(var item in dictionary)
Console.WriteLine($"{item.Key} appears {item.Value} times");
Full Demo Here
The first idea I had was the same of the comment from Jon Skeet, since the simplicity it implies.
The idea is to set null for the value we have already counted (matched).
From a developer point of view it is very simple and doesn't deviate too much from the OP's code.
Console.Write("Number of elements in the array: ");
int numberOfElements = int.Parse(Console.ReadLine());
int?[] array = new int?[numberOfElements];
for (int i = 0; i < numberOfElements; i++)
{
Console.Write($"Element no {i + 1}: ");
array[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < array.Length; i++)
{
int count = 0;
int? current = array[i];
if (array[i] != null)
{
for (int j = 0; j < array.Length; j++)
{
if (current == array[j])
{
count++;
array[j] = null;
}
}
Console.WriteLine($"{current} appears " + count + " times");
}
}
int?[] defines a nullable value type. Therefore each item in the array can have either a null or int value - documentation here.
An approach using Dictionary with O(n) complexity.
Console.Write("Number of elements in the array: ");
int numberOfElements = int.Parse(Console.ReadLine());
var dictionary = new Dictionary<int, int>();
for (int i = 0; i < numberOfElements; i++)
{
Console.Write($"Element no {i + 1}: ");
var value = int.Parse(Console.ReadLine());
if (!dictionary.ContainsKey(value)) dictionary.Add(value, 0);
dictionary[value] = dictionary[value] + 1;
}
foreach (var item in dictionary)
{
Console.WriteLine($"{item.Key} appears {item.Value} times");
}
One simple way would be to swap your outer for loop with a foreach using a set to obtain distinct values.
So replace this:
for (int i = 0; i < array.Length; i++)
With this:
foreach (int i in new HashSet<int>(array))
And this:
if (array[i] == array[j])
With this:
if (i == array[j])
Other approach more suited for you would be too take only unique values from array, i.e.:
var unique = array.Distinct().ToArray();
for (int i = 0; i < unique.Length; i++)
{
int count = 0;
for (int j = 0; j < array.Length; j++)
{
if (array[i] == array[j])
{
count++;
}
}
Console.WriteLine($"{unique[i]} appears " + count + " times");
}
In the inner loop, try to check if there were already any occurrences of the current element until you exceed the outer index.
for(int i = 0; i < array.Length-1; i++)
{
int count = 1;
bool appeared = false;
for(int j = 0; j < array.Length; j++)
{
// until we are not at the same index as the outer loop
// check if we haven't already met the current element
if(j < i)
{
if (array[i] == array[j])
{
// set current value appearance to true
// to know if current element should be displayed
appeared = true;
// break the loop because there is no sense of continuing
// current look
break;
}
}
// if we are ahead of outer index
// check if there are occurences of the element
else if(j > i)
{
if (array[i] == array[j])
count++;
}
}
// don't print the current element if it has appeared before
if(!appeared)
Console.WriteLine($"{array[i]} appears {count} times");
}
I believe there should be a more optimal solution, as this one's time complexity is linear... You can think of some optimization. For example, you can store occurred elements in the array and check through the array at each iteration, so you don't need to start the inner loop from the beginning, but instead start it from the outer loop's position + 1 but it's also not the best solution.
P.S check out about string interpolation, because you don't need to concatenate strings when you use it.
You can also use Lookup here:
var sb = new StringBuilder();
foreach (var value in array.ToLookup(item => item))
{
sb.AppendLine($"{value.Key} appears " + value.Count() + " times");
}
Console.WriteLine(sb.ToString());

How to set a range for elements within an array in C#

I´m still very new to C#, we are supposed to use for-loops not methods. My question is how to limit values (1 to 25) that users puts in.
"The game" is supposed to ask a user to put in his values then it throw´s a random number and checks if it´s among numbers kept in myArray.
{
int[] myArray = new int[10];
Console.WriteLine("Write down your numbers");
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Type in values for index nr {0}: ", i);
//user is asked to put in 10 values, what I need to do is to limit and
save as elements values in range between 1 and 25, so the loop continues (i++), if user types in a number that is outside that range my loop supposed to go a step back as if (i--)
myArray[i] = int.Parse(Console.ReadLine());
}
var rand = new Random(); //a random number is thrown
int rand1 = Convert.ToInt32(rand.Next(0, 25)); //random number ranges between 0 and 25
Console.WriteLine("{0}", rand1);
for (int i = 0; i < tal.Length; i++) //checking if random number is among users inputs
{
if (tal[i] == rand1)
{
Console.WriteLine("The random number {0} is among
your values", rand1);
}
else
{
Console.WriteLine("Random number isn´t among your
numbers");
break;
}
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Type in values for index nr {0}: ", i);
if (int.TryParse(Console.ReadLine(), out int result) && result >= 0 && result <= 25)
{
myArray[i] = result;
}
else
{
i--;
Console.WriteLine("Wrong number!");
}
}
Your issue is that you try to maintain an index i that indicate your current position in the array.
One of the solution will be to simply drop it and add item to a collection till you have 10 items.
Here I took a HashSet<int> because we can't have duplicate in HashSet.
Any time you try to add one, it will simply drop it. But If you want to allow duplicate you can use a simple List<int>.
int collectionSize = 10;
int lowerBound = 1, upperBound = 25;
var userSelectedNumbers = new HashSet<int>();
while (userSelectedNumbers.Count() < collectionSize)
{
Console.WriteLine($"Enter an int between {lowerBound} and {upperBound} : ");
var input = Console.ReadLine();
if (int.TryParse(input, out int value)
&& lowerBound <= value
&& value < upperBound
)
{
userSelectedNumbers.Add(value);
}
else {
Console.WriteLine("Not a valid input!");
}
}
Console.Clear();
Console.WriteLine(
$"userSelectedNumbers contains {userSelectedNumbers.Count()} elements : {"
+ string.Join(", ", userSelectedNumbers) + "}"
);
You can then validate if the random numer is within the range using Contains :
var random = new Random();
int computerPick = random.Next(0, 25);
Console.WriteLine("Computer picked : " + computerPick);
if (userSelectedNumbers.Contains(computerPick))
{
Console.WriteLine("You win!");
}
else {
Console.WriteLine("You loose!");
}
Don't forget the using :
using System.Collections.Generic;
using System.Linq;
nb: The range is define using 1 ≤ x < 25, using W. Dijkstra convention http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF

Numbers counter

I need help with two similar programs about arrays.
The first program is about that the user can enter any number of numbers between 0 and 9 (input can be made by the
Entering "-1" will be terminated).
After the input is finished, it should be output, how often each number between 0 and 9 was entered.
The 2nd program is about to enter 10 names and save them in a string array. After entering should first all names be output. After that, only those namesare to be issued which were entered more than once.
The Code for the 1. Program :
int cnt = 0;
int input;
while (true)
{
cnt++;
Console.WriteLine("Geben Sie bitte die {0,1}. Zahl ein (-1 für Ende):", cnt);
input = Convert.ToInt32(Console.ReadLine());
int[] count = new int[10];
int[] num = new int[cnt];
if (input > 9)
{
break;
}
else if (input == -1)
{
//Loop through 0-9 and count the occurances
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < num.Length; y++)
{
if (num[y] == x)
count[x]++;
}
}
//For displaying output only
for (int x = 0; x < 10; x++)
Console.WriteLine("Number " + x + " appears " + count[x] + " times");
And for the 2nd Program :
int cnt = 10;
string[] name = new string[11];
for (int i = 1; i < 11; i++)
{
Console.WriteLine("Name Nr.{0,1} eingeben: ", i);
name[i]++;
name[i] = Console.ReadLine();
}
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < name.Length; y++)
{
if (i == x)
{
//For displaying output only
for (int a = 0; a < 10; a++)
Console.WriteLine("Folgende Namen wurden mehrfach eingegeben : ", name[i]);
break;
The Problem for the 1st Program is that if i type "-1" The number 1-9 always shows that it appeared 0 times and the number 0 always as an example 4 times when i type 4 numbers.
And for the 2nd is that i really dont know how to put strings in arrays. I want to know how to do that, because of the similarity of these 2 programs.
First of all: don't initialize the arrays every loop iteration. Initialize them on the beginning.
Second thing: you must count the numbers before inputting -1. See that you try to increase the count when you enter -1 whereas -1 meant to be program termination I'm assuming.
And third: ask the second problem with string array as another question. You'll get both answers faster that way.
Here's few alteration of your code that needs to be done.
int input;
int[] count = new int[10];
while (true)
{
Console.WriteLine("Geben Sie bitte die {0,1}. Zahl ein (-1 für Ende): ");
input = Convert.ToInt32(Console.ReadLine());
if(input >= 0 && input <= 9)
{
for (int x = 0; x < 10; ++x)
{
if(x == input)
{
count[x] += 1;
}
}
}
else if (input == -1)
{
//Input finished, display of numbers appearances.
for (int x = 0; x < 10; x++)
Console.WriteLine("Number " + x + " appears " + count[x] + " times");
break;
}
else
{
break;
}
}

Why am I getting an IndexOutOfRangeException in this code?

I basically need to store an array of any amount of contents to an integer but then I have to echo it all out after.
I am getting an indexoutofrange error.
for (int index = 0; index < userArray; index++, userArray--)
{
Console.WriteLine("Number " + userArray + " Value is:");
userArrayinputed[userArray] = int.Parse(Console.ReadLine());
}
All the Code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is the Size of the Array?");
string inputArray = Console.ReadLine();
int userArray = Convert.ToInt32(inputArray);
int[] userArrayinputed = new int[userArray];
for (int index = 0; index < userArray; index++, userArray--)
{
Console.WriteLine("Number " + userArray + " Value is:");
userArrayinputed[userArray] = int.Parse(Console.ReadLine());
}
for (int index = userArray; index > 0; index--, userArray--)
{
Console.WriteLine(userArrayinputed);
Console.ReadLine();
}
Correct Code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is the Size of the Array?");
string inputArray = Console.ReadLine();
int userArray = Convert.ToInt32(inputArray);
int maxArray = userArray;
int[] userArrayinputed = new int[userArray];
for (int index = 0; index < userArray; index++)
{
Console.WriteLine("Number " + index + " Value is:");
userArrayinputed[index] = int.Parse(Console.ReadLine());
}
for (int index = 0; index < userArray; index++)
{
Console.WriteLine(userArrayinputed[index]);
Console.ReadLine();
}
So, array's are zero based for indexing, that means if you want an array of 10 then the indexers are going to be 0-9.
so when you go up an array (0-9) you want the top of a for loop to be <(less than the array length) when you are going down the array (9-0) you want the lower bound to be >= 0 (less than or equal to the bottom of the array) otherwise you will start out trying to access at 10 (the array length) and get an OutOfRangeException.
for example:
for (int i = 0; i < myArray.Length -1; i++)
{ ... }
and
for (int i = myArray.Length - 1; i >= 0; i--)
{ ... }
and when you are displaying the index in a for loop you will want to display the index and not the array length.
Also something of note - you were deducting the value of the userArray variable in two separate for loops, which does not reset it when it leaves the loop, so at the end of the method, the userArray variable would have been at -(2*userArray) instead of what I think you were going for which was the index/array length.
So it would look something like this
static void Main(string[] args)
{
Console.WriteLine("What is the Size of the Array?");
string inputArray = Console.ReadLine();
int userArray = Convert.ToInt32(inputArray);
int[] userArrayinputed = new int[userArray];
for (int index = 0; index < userArray; index++)
{
Console.WriteLine("Number " + index + " Value is:");
//note you will get an error here if you try and parse something that isn't an interger
userArrayinputed[index] = int.Parse(Console.ReadLine());
}
for (int index = userArray -1; index >= 0; index--)
{
Console.WriteLine(userArrayinputed[index]);
}
Console.ReadLine();
}
index of array is starting from 0 to (userArray -1)
string inputArray = Console.ReadLine();
int userArray = Convert.ToInt32(inputArray);
int[] userArrayinputed = new int[userArray];
for (int index = 0; index < userArray; index++)
{
Console.WriteLine("Number " + index+ " Value is:");
userArrayinputed[index] = int.Parse(Console.ReadLine());
}
for (int index = 0; index < userArray; index++)
{
Console.WriteLine(userArrayinputed[index]);
Console.ReadLine();
}
Replace
userArrayinputed[userArray]
with
userArrayinputed[index]
and remove the userArray-- from your for loops. Use the index instead to display the current number:
for (int index = 0; index < userArray; index++)
{
Console.WriteLine("Number " + index + " Value is:");
...
Initially, userArrayinputed[userArray] accesses the last index which is userarray-1
since userarray is the length of the array
here is the fix,
for (int index = 0; index < userArray; index++, userArray--)
{
Console.WriteLine("Number " + userArray + " Value is:");
userArrayinputed[userArray-1] = int.Parse(Console.ReadLine());
}
Cause
Your for() loop is first evaluated with userArray == userArrayinputed.Length, so
userArrayinputed[userArray] tries to access the (N + 1)th element of the array which leads to the IndexOutOfRangeException.
Array indexing starts with 0, remember? So an array with say, 3 items
int[] arr = new int[3]
the array has the items arr[0], arr[1] and arr[2] but no arr[3] which would be the 4th item.
Solution
Just use userArrayinputed[index] and remove the userArray-- part in the for() statement.
If you still want to enter the numbers in reverse order you can count down from userArray-1:
for (int index = userArray - 1; index >= 0; index--)
{
Console.WriteLine("Number " + index + " Value is:");
userArrayinputed[index] = int.Parse(Console.ReadLine());
}
To display the numbers, you should WriteLine the array elements, not the array:
for (int index = userArray - 1; index >= 0; index--)
{
Console.WriteLine(userArrayinputed[index]);
Console.ReadLine();
}

Array Duplicate Elimination with c#?

I have a program here that need some improvements. This Program inputs 5 elements in an Array and Removes if any duplicates. It works but the problem is that it sets every duplicate to zero. I don't want to display zero. I want it completely destroyed and eliminated. I don't want that duplicate element to appear. This is what I have so Far! Could Use some help. Thank You.
// Gurpreet Singh
// Duplicate Program
using System;
class duplicate
{
static void Main()
{
const int Array_Size = 5;
int [] number = new int [Array_Size];
int i;
for ( i = 0; i < Array_Size; i++)
{
Console.Write("Element " + i + ": ");
number[i] = Int32.Parse(Console.ReadLine());
if (number[i] < 9 || number[i] > 101)
{
Console.WriteLine("Enter Number between 10 - 100");
number[i] = Int32.Parse(Console.ReadLine());
}
}
for (i = 0; i < Array_Size; i++)
{
for (int j = 0; j < Array_Size; j++)
{
if (i != j)
{
if (number[j] == number[i])
number[j] = 0;
}
}
}
Console.WriteLine("Duplicate Removed:");
for (i = 0; i < Array_Size; i++)
{
Console.WriteLine("Element " + i + " " + number[i]);
}
Console.ReadLine();
}
}
The easiest way is to use Linq's Distinct method:
number = number.Distinct().ToArray();
This will return a new array without any duplicates.
The duplicate is displayed as zero, since you assign the value of the duplicate to be zero, in the line,
if(number[j]==number[i])
number[j]=0
to delete the element from the array, use the following code:
if(number[j]==number[i])
{
int k=j;
while(k<Array_Size-1)
{
number[k]=number[k+1];
k++;
}
Array_Size--;
}
the statement Array_Size--; is done so that the last element is not repeated twice
This is my complete code in which I put some double-for-loop statement to
prevent it from inserting the duplicated integers in an array.
Have a look.
class Program
{
static void Main(string[] args)
{
const int ARRAY_SIZE = 5;
int[] ArrayTable = new int[ARRAY_SIZE];
int Element=0;
int a;
for(a=0; a<ArrayTable.Length;a++)
{
Console.Write("Please Enter an integer (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
while (Element < 10 || Element > 100)
{
Console.Write("Try again (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
}
ArrayTable[a] = Element;
for (int b = 0; b < a; b++)
{
while (ArrayTable[a] == ArrayTable[b])
{
Console.Write("Integer Duplicated!\nTry again: ");
Element = Int32.Parse(Console.ReadLine());
ArrayTable[a] = Element;
Console.WriteLine();
while (Element < 10 || Element > 100)
{
Console.Write("Try again (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
ArrayTable[a] = Element;
}
}
}
}
for (int c = 0; c < ArrayTable.Length; c++)
{
Console.Write("{0} ", ArrayTable[c]);
}
}

Categories