Non-unique Elements
Non-unique Elements
You are given a non-empty list of integers (X). For this task, you should return a list consisting of only the non-unique elements in this list. To do so you will need to remove all unique elements (elements which are contained in a given list only once). When solving this task, do not change the order of the list. Example: [1, 2, 3, 1, 3] 1 and 3 non-unique elements and result will be [1, 3, 1, 3].
题目大义:将数组中唯一的元素清除
还是C语言的思想,直接粗暴,首先是遍历求出唯一的元素,再使用数组的remove方法
1 def checkio(data):
2
3 only = []
4
5 for each in data:
6 count = 0
7 for other in data:
8 if each == other:
9 count += 1
10
11 if count == 1:
12 only.append(each)
13
14 for each in only:
15 data.remove(each)
16
17 return data
当然我们有更高级的方法list = [x for x in data if x.count > 1],简单明了,使用了数组的另一种构造方法,使用了数组count方法
1 def checkio(data):
2 list = [x for x in data if data.count(x) > 1]
3 return list
新技能get
Non-unique Elements的更多相关文章
- Ruby: Count unique elements and their occurences in an array
Is there a method in Ruby that takes an array, and counts all unique elements and their occurrences ...
- [LeetCode] Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- (Collection)347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- Leetcode 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- 347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [LeetCode] 347. Top K Frequent Elements 解题思路 - Java
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 347. Top K Frequent Elements 最常用的k个元素
[抄题]: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1 ...
随机推荐
- cache buffers chains latch
cache buffers chains latch 从 Oracle 8i Database 开始, 散列锁存器<-------(1:m)------>hash bucket<-- ...
- Android App Widget的简单使用
App Widget是一些桌面的小插件,比如说天气和某些音乐播放应用,放到桌面去的那部分: 例如: 实现步骤及代码如下: (1)首先,在AndroidManifest.xml中声明一个App Widg ...
- struct2(二) struct2的hello world 程序
在struct2 的web应用程序中,当你点击一个超链接或者提交一个HTML页面的时候,并不是直接的转向一个另一个的页面,而是转到你提供的一个Java 类.这个过程被称为一个action,一个acti ...
- 关于bootstrap--列表(ol、ul)
1.list-unstyled : 在<ol>(有序列表)</ol><ul>(无序列表)</ul>中加入class="list-styled& ...
- myeclipse实现Servlet实例(1) 通过继承servlet接口实现
1.在myeclipse新建web project,配置Tomcat(在myeclipse的Window--preferences) 2.然后在src新建servlet文件( 此处放在com.tsin ...
- 搭建Android环境
1.相关文件下载: 1.1.Java jdk下载: JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jre7-downl ...
- 信息熵(Entropy)究竟是用来衡量什么的?
信息熵(Entropy)究竟是用来衡量什么的? ——与Philip ZHANG商榷 思明 Philip ZHANG先生在反驳彭小明的时候,提出一个观点,他说:“ 就语言文 字来说,总体效率不是用民族主 ...
- 用Spring提高java观察者模式灵活性
在上篇博客 用java观察者模式解耦经典三层架构 的最后,用了一个Client类把Listener的实现类注冊到了LoginEventSource类中,假设须要加入�新的逻辑,加入�新的listene ...
- mysql判断某个字符串是否包含另一个
like SELECT * FROM wx_webauth_userinfo where city LIKE "%台%";"; 结果: 函数find_in_set(str ...
- KVO的概述的使用
一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应 ...