Python Forum
The int that appears an odd number of times.
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The int that appears an odd number of times.
#1
Hello Guys,

Here i am having a array

array=[1,2,3,2,4,2,4,3,5,5,1]
I just need to find out which number that appears an odd no of times

For this array the output should be:
Output:
2
How should do that?
Reply
#2
Please provide us with your attempt.
Reply
#3
i did with dictionary
a = [1,2,3,2,4,2,4,3,5,5,1]
result = dict((i, a.count(i)) for i in a)
print result
I just got the counts but how to print only the int that appears an odd no of times
Output:
{1: 2, 2: 3, 3: 2, 4: 2, 5: 2}
Reply
#4
hint
if number % 2:
Reply
#5
1 appears 1 time and 1 is odd

what if ...

a = [1,2,3,2,4,2,3,5,3]
lots of oddities!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
Yeah You are right skaperen!
Now i changed my input to a new one,i written code also it works as expect
a = [1,2,3,2,4,2,4,3,5,5,1]
result = dict((i, a.count(i))for i in a)
for i in result:
if a.count(i)%2!=0:
 print "The int that occur odd no of times:",i
Output:
The int that occur odd no of times: 2
Reply
#7
I think '&' operator will be faster
In [1]: %time 5 % 2 == 1
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 4.05 µs
Out[1]: True

In [2]: %time 5 & 1 == 1
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 3.1 µs
Out[2]: True
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  times greater number than the previous rhai 4 3,435 Oct-08-2018, 03:27 AM
Last Post: woooee
  when I type either C B or A nothing appears Samp97 3 2,800 Aug-15-2018, 06:36 AM
Last Post: DuaneJack
  Find how many times a user played an artist and how many times disruptfwd8 1 2,584 May-04-2018, 08:32 AM
Last Post: killerrex
  Search for Player ID and return games played and number of times disruptfwd8 3 3,409 May-03-2018, 06:45 PM
Last Post: disruptfwd8

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020