java中怎么样子找出数组中重复的数,并去除 Java数组中重复数据的删除
java\u4e2d\u600e\u6837\u53bb\u9664list\u4e2d\u91cd\u590d\u7684\u6570\u636e\uff0c\u5e76\u4e14\u663e\u793a\u91cd\u590d\u6570\u636e\u7684\u6761\u6570\u5f88\u591a\u96c6\u5408\u5904\u7406\uff0c\u6211\u90fd\u63a8\u8350\u7528Java8\u7684\u6d41\u6765\u5904\u7406\uff0c\u662f\u975e\u5e38\u7b80\u5355\u6613\u61c2\u7684
\u5176\u5b9e\u4f60\u8fd9\u4e2a\u95ee\u9898\uff0c\u603b\u6765\u8bf4\u5c31\u662f\u6309\u7167\u96c6\u5408\u4e2d\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u5206\u7c7b\uff0c\u5e76\u7edf\u8ba1\u6570\u91cf\uff0cStream\u53ea\u7528\u7b80\u5355\u7684\u4ee3\u7801\u5c31\u5b8c\u6210\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u4ee3\u7801\u5982\u4e0b
List list = Arrays.asList("day", "walipo", "day", "walipo", "legeand", "day", "douwbo", "day", "day");Map collect = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));System.out.println(collect);
\u6700\u540e\u8fd4\u56de\u7684Map\u5c31\u662f\u4ee5\u5b57\u7b26\u4e32\u4e3akey\uff0cvalue\u4e3a\u5176\u4e2a\u6570
java\u6570\u7ec4\u4e2d\u53bb\u6389\u91cd\u590d\u6570\u636e\u53ef\u4ee5\u4f7f\u7528set\u96c6\u5408\uff0cset\u672c\u8eab\u5185\u90e8\u662f\u4e0d\u4fdd\u5b58\u91cd\u590d\u7684\u6570\u636e\u7684\uff0c\u5982\u4e0b\u4ee3\u7801\uff1a
import java.util.Arrays;import java.util.Set;import java.util.TreeSet;public class ceshi {public static void main(String[] args) {int[] testArr = { 5, 5, 1, 2, 3, 6, -7, 8, 6, 45, 5 };//\u65b0\u5efa\u4e00\u4e2aint\u7c7b\u578b\u6570\u7ec4System.out.println(Arrays.toString(testArr));Set set = new TreeSet();//\u65b0\u5efa\u4e00\u4e2aset\u96c6\u5408for (int i : testArr) {set.add(i);}Integer[] arr2 = set.toArray(new Integer[0]);// \u6570\u7ec4\u7684\u5305\u88c5\u7c7b\u578b\u4e0d\u80fd\u8f6c \u53ea\u80fd\u81ea\u5df1\u8f6c\uff1b\u5427Integer\u8f6c\u4e3a\u4e3aint\u6570\u7ec4\uff1bint[] result = new int[arr2.length];for (int i = 0; i < result.length; i++) {result[i] = arr2[i];}System.out.println(Arrays.toString(arr2));}}\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a
public static void main(String[] args) {
//可以换种思路,把数组放到set里面(set的值不会重复)就可以去重了
Integer[] arr = {85,4,2,6,11,4,5,8,9};
Set<Integer> set = new HashSet<Integer>();
for(Integer i : arr)
set.add(i);
for(Object j: set.toArray())
System.out.print(j + " ");
}
使用Set集合处理即可,因为Set集合有特性,自动去除重复的元素;
只要循环数组,添加到set中就可以实现了。
思想大概就是新建一个数组,然后把原数组的值放入新数组,检查有没有已存在的,放不进去的就是重复的,新数组就是你需要的
恩,把数组转换成set集合再转回去就行,
遍历数组,将值放入TreeSet集合,这个集合不允许重复,而且这个效率比较高
绛旓細1銆佸啓涓や釜for寰幆锛屽灞俧or寰幆姣忓彇鍑轰竴涓硷紝閮藉拰鍐呭眰for寰幆鐨勫煎仛姣旇緝锛屽嚭鐜颁袱娆″強浠ヤ笂鐩稿悓鐨勬儏鍐锛屽嵆鏈夐噸澶嶅厓绱犮傚綋鐒惰繖绉嶆晥鐜囨瘮杈冧綆銆2銆佸啓涓や釜for寰幆锛屽灞俧or寰幆鍙栫涓涓肩殑鏃跺欙紝鍜屽唴灞傚惊鐜殑绗簩涓煎強涔嬪悗鐨勫兼瘮杈冿紱澶栧眰for寰幆鍙栫浜屼釜鍊肩殑鏃跺欙紝鍜屽唴灞傚惊鐜殑绗笁涓煎強涔嬪悗鐨勫兼瘮杈...
绛旓細浣跨敤涓涓复鏃秎ist锛屽熷姪list.contains鍒ゆ柇褰撳墠鏁扮粍鐨勫兼槸鍚﹀瓨鍦ㄤ簬list涓锛屽鏋滀笉瀛樺湪鍒欏姞鍏ュ埌list涓紝濡傛灉瀛樺湪鍒欒褰曞嚭鏉ャ傛湁鐐瑰儚鎶婁竴涓瀛愪腑鐨勬鐞冩斁鍒板彟涓涓┖绡瓙涓紝濡傛灉绡瓙閲岄潰宸茬粡鏈夌殑灏变笉瑕佸啀鏀捐繘鍘讳簡锛屾病鏈夌殑锛屼竴涓釜鏀捐繘鍘汇傝鍒板簳灏辨槸涓亶鍘嗗拰瀛樺湪鎬у垽鏂棶棰樸
绛旓細鍏跺疄鍙閬嶅巻鏁扮粍锛岀劧鍚庢斁杩泂et闆嗗悎鍐呭嵆鍙疄鐜銆傛瘮濡傦細//set闆嗗悎鍙互鑷姩鍘婚噸 Integer[] a = {1, 2, 2 , 19, 19, 8, 9};Set<Integer> set = new HashSet<Integer>();for(Integer i : a)set.add(i);for(Object s: set.toArray())System.out.print(s+ " ");} ...
绛旓細涓绉嶆柟娉曪紝鍐欎袱涓祵濂楃殑for寰幆锛屽皢鏁扮粍閬嶅巻锛岃繕鏈変竴绉嶆柟娉曞氨鏄埄鐢╯et鐨勫厓绱犱笉閲嶅鎬э紝璁叉暟缁勪腑鐨勫厓绱犳斁鍏et锛岀劧鍚庡啀瀵规瘮锛屽皯鐨勫氨鏄噸澶嶇殑
绛旓細public static void main(String[] args) { //鍙互鎹㈢鎬濊矾锛屾妸鏁扮粍鏀惧埌set閲岄潰锛坰et鐨勫间笉浼氶噸澶嶏級灏卞彲浠ュ幓閲嶄簡Integer[] arr = {85,4,2,6,11,4,5,8,9};Set<Integer> set = new HashSet<Integer>(); for(Integer i : arr) set.add(i); for(Object j: set.toArray()) System....
绛旓細棣栧厛锛岀敤java涓鐨勬湁搴忕殑Array锛屼綘鏍规嵁鑷繁闇瑕侀噸鍐檆ompare鏂规硶锛岀涓涓棶棰樺氨鏄寜鐓ф暟瀛楃殑澶у皬鎺掑簭锛岀浜屽氨鏄寜鐓у瓧绗︿釜鏁版帓搴忥紙鏈夊簭鐨凙rray鍦ㄥ缓绔嬭繃绋嬩腑宸茬粡姝g‘鎺掑簭浜嗭級锛岃繖鏍峰氨寰楀埌涓や釜鏈夊簭鏁扮粍銆傜浜岋紝澶畝鍗曚簡锛岃嚜宸辨濊冨惂銆備竴涓惊鐜悶瀹氾紝澶嶆潅搴锛坣锛夈傛帓搴忓鏉傚害涔熸槸O(n)锛屾墍浠ヨ繖涓や釜棰樺鏉...
绛旓細JAVA閲岄潰鍙互鍒╃敤Set Map鐨勫敮涓鎬ф潵鎺掗櫎閲嶅鏁板硷紝鍏蜂綋浠g爜濡備笅锛歩mport java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.TreeSet;public class Test { public static void main(String[] args) { int[] ary = {2,5,4,7,8,9,6,4...
绛旓細public class Test2 { public int produceRandom() { // 鐢ㄦ埛杈撳叆鐨勬暟瀛椾笌闅忔満浜х敓鐨勫厬濂栧彿鐮佺浉鍚岀殑鎬绘暟 int sum = 0; // 鏀鹃殢鏈轰骇鐢熸暟瀛楃殑鏁扮粍 int[] arrayInt = new int[5]; // 鏀鹃殢鏈轰骇鐢5涓暟瀛楀苟涓旀斁鍦鏁扮粍涓 for (int i = 0; i < 5; i++) { Random...
绛旓細浣犲彲浠ョ敤map 绫诲瀷锛屾濊矾澶ф鏄繖鏍风殑锛屼唬鐮佹垜灏变笉鍐欎簡銆傛妸鏁扮粍浠庣0涓紑濮 淇濆瓨鍦<key,value>閲 浣滀竴涓姣旇繃绋 鍏朵腑key琛ㄧず鍏冪礌锛屽瓨鐨勮繃绋嬩綔涓涓姣 濡傛灉鐩稿悓鍒欏叾鐩稿簲鐨剉alue鍊+1.鏈鍚庤緭鍑簁ey锛歷alue.
绛旓細public static boolean judge(String[] str1) { //鍋囪str鏁扮粍涓轰互涓嬫暟鎹 String[] str = {"ssdzca","zf","zi"};for (int i = 0; i < str.length - 1; i++) { char[] ca = str[i].toCharArray();char[] ca2 = str[i+1].toCharArray();for (int j = 1; j < ca....