0

I just wanna ask what best way to work around a Two-Dimensional Array (2 Columns) which would store: CandidateName and their respective VoteCount.

What I want exactly to do is to, accept an input from the user say: VOTE John 10 wherein John is the name of the candidate and 10 is the votes that he wanna give him. So I need to store {John, 10} into my array. However, after this my program would once again ask the user for votes so if I enter VOTE Doe 15, the entry {Doe, 15} would then be added to the array. If the user enters VOTE John 2, my array needs to be updated and thus the new value would be {John, 12}.

Currently I use two arraylists: CandidateName and VoteCount and I just rely on their index for pairing. However, this isn't really reliable so I'm trying to find another way on how to solve this. However, I'm not really a big fan of multi-dimensional arrays.

Can someone please point me out to a good way on how to achieve this?

8 Answers 8

5
public class VoteManager
{
    public Dictionary<string, int> Votes { get; private set; }
    public VoteManager
    {
        Votes = new Dctionary<string, int>();
    }
    public void AddVotes(string name, int voteCount)
    {
        int oldCount;
        if (!Votes.TryGetValue(name, out oldCount))
            oldCount = 0;
        Votes[name] = oldCount + voteCount;
    }
Sign up to request clarification or add additional context in comments.

Comments

3

You should use an Associative Array. In the case of C#, such a collection is the Dictionary.

var votes = new Dictionary<string, int>();
votes["John"] = 10;
votes["Bob"] = 20;
votes["John"] = 15; // replaces earlier setting

If you want to add to the exisiting vote, you will need to check if there is an existing value:

private Dictionary<string, int> votesByPeep; // initialized in constructor

private void AddVotes(string peep, int votes)
{
    if (this.votesByPeep.ContainsKey(peep)
    {
        this.votesByPeep[peep] += votes;
    }
    else
    {
        this.votesByPeep[peep] = votes;
    }
}

Comments

1

Why don't you define a struct/class with two properties, Name and VoteCount. Then you only need one array.

EDIT:

I suggested this because there may be additional operations or properties you want to add to Candidates. If all you need is an association between these two values, a dictionary is the correct solution.

Comments

1

It sounds like a much better solution here is to use a Dictionary<TKey, TValue>. A dictionary / hashtable is ideal for a scenario where you're pairing a value (vote count) with a given key (user name). It makes for very easy update and lookup scenarios

class Container {
  private Dictionary<string, int> m_voteMap = new Dictionary<string, int>();

  public void SetVote(string user, int votes) {
    m_voteMap[user] = votes;
  }

  public int GetVotes(string user) {
    int votes;
    if (!m_voteMap.TryGetValue(user, out votes)) {
      votes = 0;
    }
    return votes;
  }
}

Comments

1

You can use a dictionary from strings (names) to int (votes), this will give you the {name, votes} pair and a nice quick lookup

Comments

0

Create a class called CandidateVotes, and store that in a List<CandidateVotes> collection.

public class CandidateVotes
{
    public string Name {get; set;}
    public int Votes {get; set;}
}

Comments

0
Dictionary<string, int> is your friend

Comments

0

This sounds like a good candidate for a Dictionary<T,U>. In this case, Dictionary<string,int>, with the key being the candidate, and the value being the vote count.

// Create dictionary as:
Dictionary<string, int> votes = new Dictionary<string, int>();

You could then make some routines like the following:

void AddVotes(string candidate, int numberOfVotes)
{
    if (this.votes.Contains(candidate))
    {
         // Update the "10 to 12" in your scenario
         int current = this.votes[candidate];
         current += numberOfVotes;
         this.votes[candidate] = current;
    }
    else
         this.votes[candidate] = numberOfVotes; // First time a candidate is used...
}

When you want to list out the votes per candidate, you can do something like:

foreach(var pair in this.votes)
{
    Console.WriteLine("Candidate {0} has {1} votes.", pair.Key, pair.Value);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.