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 ...
随机推荐
- 价格更低、SLA 更强的全新 Azure SQL 数据库服务等级将于 9 月正式发布
继上周公告之后,很高兴向大家宣布更多好消息,作为我们更广泛的数据平台的一部分, 我们将在 Azure 上提供丰富的在线数据服务.9 月,我们将针对 Azure SQL 数据库推出新的服务等级.Azur ...
- javascript 典型闭包的用法
<body><input type="radio" id="radio1" name="readionGroup" /&g ...
- Java中的enum
package com.st.java; /** * ENUM枚举类型的使用 * @author Administrator * 2016年04月10日 */ public enum MoneyTyp ...
- testlink 下载地址
testlink 下载地址 https://sourceforge.net/projects/testlink/files/TestLink%201.9/
- 删除ubuntu旧内核
ubuntu的内核经常升级,而老内核并不自动卸载.时间长了,就有一大堆内核垃圾,需要我们手动去清理. 先用uname -a 查看当前内核版本: xzc@xzc-HP-ProBook-4446s:~$ ...
- [Redux] Redux: Extracting Container Components -- AddTodo
Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...
- 用递归翻转一个栈 Reverse a stack using recursion
明白递归语句之前的语句都是顺序运行,而递归语句之后的语句都是逆序运行 package recursion; import java.util.Stack; public class Reverse_a ...
- Citrix 服务器虚拟化之九 Xenserver虚拟机的XenMotion
Citrix 服务器虚拟化之九 Xenserver虚拟机的XenMotion XenMotion 是 XenServer 的一项功能,能够将正在运行的虚拟机从一台 XenServer 主机上迁移到另外 ...
- Hacker(17)----认识Windows系统漏洞
Windows系统是迄今为止使用频率最高的操作系统,虽然其安全性随着版本的更新不断提高,但由于人为编写的缘故始终存在漏洞和缺陷.但Mircosoft公司通过发布漏洞补丁来提高系统的安全性,使Windo ...
- oracle卸载Oracle Clusterware(转载)
1.脚本自动删除 切换到root用户 $Su – root #cd $ORA_CRS_HOME/install 1.执行rootdelete.sh脚本 # ./rootdelete.sh 2.执行ro ...