Detect Voter Fraud in Python



Suppose we have a list of votes, where each element in the list has two elements [c_id, v_id], the c_id is the candidate id and v_id is the voter id. We have to check whether any voter has voted more than once or not.

So, if the input is like [[5, 1],[5, 0],[5, 4],[5, 3],[5, 0]], then the output will be True as [5,0] is present twice

To solve this, we will follow these steps −

make a new set named all

  • for each vote in votes, do
    • insert (vote[1]) into all
  • return true when size of all is not same as size of votes

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, votes):
      all = set()
      for vote in votes:
         all.add(vote[1])
      return len(all) != len(votes)
ob = Solution()
votes = [[5, 1],[5, 0],[5, 4],[5, 3],[5, 0]]
print(ob.solve(votes))

Input

[[5, 1],[5, 0],[5, 4],[5, 3],[5, 0]]

Output

True

Advertisements