Posts: 22
Threads: 13
Joined: Dec 2016
Dec-30-2016, 05:47 AM
(This post was last modified: Dec-30-2016, 07:51 AM by MeeranRizvi.)
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?
Posts: 591
Threads: 26
Joined: Sep 2016
Please provide us with your attempt.
Posts: 22
Threads: 13
Joined: Dec 2016
Dec-30-2016, 06:36 AM
(This post was last modified: Dec-30-2016, 07:53 AM by MeeranRizvi.)
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}
Posts: 12,046
Threads: 487
Joined: Sep 2016
Dec-30-2016, 06:46 AM
(This post was last modified: Dec-30-2016, 06:46 AM by Larz60+.)
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
Dec-30-2016, 07:44 AM
(This post was last modified: Dec-30-2016, 07:45 AM by Skaperen.)
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.
Posts: 22
Threads: 13
Joined: Dec 2016
Dec-30-2016, 09:17 AM
(This post was last modified: Dec-30-2016, 09:17 AM by MeeranRizvi.)
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
Posts: 2,953
Threads: 48
Joined: Sep 2016
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
|