用Java删除数组中所有相同的数,使之仅剩下一个,然后输出剩下的数据? Java去除数组中的重复的数据后输出数组并把重复的数输出?
\u7f16\u5199\u7a0b\u5e8f\u5728\u88ab\u8c03\u51fd\u6570\u4e2d\u5220\u53bb\u4e00\u7ef4\u6570\u7ec4\u4e2d\u6240\u6709\u76f8\u540c\u7684\u6570\uff0c\u4f7f\u4e4b\u53ea\u5269\u4e00\u4e2a\uff0c\u88ab\u8c03\u51fd\u6570\u8fd4\u56de\u5220\u9664\u540e\u6570\u7ec4\u4e2d\u7684\u6570\u636e\u7684\u4e2a\u6570int main()
{
int a[20]={4,4,4,6,7,7,7,9,9,9,12,12,24,34,34,34,56,67,78,90};
int i=0,j=0,k=0,b[20];//b[]\u4e3a\u4fdd\u5b58\u4e0d\u76f8\u540c\u7684\u6570\u7ec4\u6210\u5458
for(i=0;i<20;i++)//\u904d\u5386\u6bcf\u4e2a\u6570\u7ec4\u6210\u5458
{
for(j=0;j<k;j++)//\u6570\u7ec4\u6210\u5458\u4e0e\u4e0d\u540c\u6570\u7ec4\u6210\u5458\u6bd4\u8f83
{
if(b[j]==a[i])//\u5982\u679c\u4e0e\u4e0d\u540c\u6570\u7ec4\u6bd4\u8f83\u6709\u76f8\u540c\u8005\uff0c\u8df3\u51fa\uff0c\u8fdb\u884c\u4e0b\u4e00\u4e2a\u6570\u7ec4\u6210\u5458\u6bd4\u8f83
break;
}
if(j==k)//\u5982\u679c\u4e0e\u4e0d\u540c\u6570\u7ec4\u7684\u6210\u5458\u90fd\u4e0d\u540c\uff0c\u5219\u52a0\u5165\u4e0d\u540c\u6570\u7ec4\uff1b
{
b[k]=a[i];
k++;
}
}
for(i=0;i<k;i++)
printf("%d,",b[i]);
getchar();
return 0;
}
\u5c31\u662f\u518d\u63a5\u4e00\u4e2a\u65b0\u7684list
\u4f60if{}\u540e\u9762\u52a0\u4e86else\u628a\u91cd\u590d\u6570\u636e\u6dfb\u52a0\u5230\u65b0list
\u7136\u540e\u904d\u5386\u5c31\u53ef\u4ee5\u4e86
/**
* 方法1 就是申请一个和nums大小一样的数组tmpNums,
* 然后遍历nums中的元素,对每个元素判断是否在tmpNums出现过,
* 如果出现过,那么就不放到新数组里面,也就是不做处理;
* 如果没有出现过,那么就把它放到新的数组里面
* 这种方法数组原来的相对顺序可以保证
* 时间复杂度是n^2和空间复杂度是n
*
*
@param nums 输入需要去重的数组
*
@return 返回去重后数组的长度
*/
public static int unique1(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] tmpNums = new int[nums.length];
tmpNums[
0] = nums[0];
int index = 1;
for (int i = 1, len = nums.length; i < len; i++) {
int j = 0;
for (; j < index; j++) {
if (tmpNums[j] == nums[i]) {
break;
}
}
if (j == index) {
tmpNums[index
++] = nums[i];
}
}
nums
= tmpNums;
return index;
}
绛旓細闇瑕佷紶鍏ヤ竴涓狾bject鏁扮粍锛岀劧鍚庤繑鍥炲幓閲嶅悗鐨勬暟缁勶紱璁板綍鍘婚櫎閲嶅涔嬪悗鐨勬暟缁勯暱搴﹀拰缁欎复鏃舵暟缁勪綔涓轰笅鏍囩储寮曪紱鍒涘缓涓涓复鏃舵暟缁勶紱閬嶅巻鍘熸暟缁勶紱澹版槑涓涓爣璁帮紝骞舵瘡娆¢噸缃紱鍐呭眰寰幆灏嗗師鏁扮粍鐨鍏冪礌閫愪釜瀵规瘮锛涘鏋滃彂鐜版湁閲嶅鍏冪礌锛屾敼鍙樻爣璁扮姸鎬佸苟缁撴潫褰撴鍐呭眰寰幆锛涘垽鏂爣璁版槸鍚﹁鏀瑰彉锛屽鏋滄病琚敼鍙樺氨鏄病鏈夐噸澶嶅厓绱狅紱...
绛旓細mySet.add(obj);} mySet涓墍淇濆瓨鐨勫厓绱犲氨鏄敮涓鐨勪簡.鍐嶅惂mySet淇濆瓨鍒鏁扮粍涓 瀹屾暣渚嬪瓙锛// 鍒涘缓涓涓暟缁勶紝閲岄潰瀛樺湪閲嶅鐨勫厓绱 String[] myArray = {"s","s","f","d"};Set<String> mySet = new HashSet<String>();// 鍘婚櫎閲嶅鍏冪礌 for(String s : myArray){ mySet.add(s);} my...
绛旓細鏂规硶1 灏辨槸鐢宠涓涓拰nums澶у皬涓鏍风殑鏁扮粍tmpNums锛岀劧鍚庨亶鍘唍ums涓殑鍏冪礌锛屽姣忎釜鍏冪礌鍒ゆ柇鏄惁鍦╰mpNums鍑虹幇杩囷紝濡傛灉鍑虹幇杩囷紝閭d箞灏变笉鏀惧埌鏂鏁扮粍閲岄潰锛屼篃灏辨槸涓嶅仛澶勭悊锛涘鏋滄病鏈夊嚭鐜拌繃锛岄偅涔堝氨鎶婂畠鏀惧埌鏂扮殑鏁扮粍閲岄潰 杩欑鏂规硶鏁扮粍鍘熸潵鐨勭浉瀵归『搴忓彲浠ヤ繚璇 鏃堕棿澶嶆潅搴︽槸n^2鍜岀┖闂村鏉傚害鏄痭 param nums 杈撳叆...
绛旓細鏈鐩存帴鐨勬柟寮, 鐢ㄥ祵濂楀惊鐜, 浠鏁扮粍绗竴涓厓绱犲紑濮嬩笌璇ュ厓绱犱箣鍚庣殑鎵鏈鍏冪礌姣旇緝, 濡傛灉鍙戠幇鐩稿悓鐨,鍒鍒犻櫎鍚庤 杩欐槸鏂规硶:for (i=0; i < 鏁扮粍闀垮害; i++){ int temp = 鍏冪礌[i];for (int j = i +1; j < 鏁扮粍闀垮害; j++){ if (temp == 鍏冪礌[j])鍒犻櫎鍏冪礌[j];} } ...
绛旓細importjava.util.Arrays;publicclassTest2{publicstaticvoidmain(String[]args){int[]a={1,1,4,4,3,3,5,3,8,0,5,1};System.out.println("杈撳叆鐨鏁扮粍涓:"+Arrays.toString(a));a=Arrays.stream(a).distinct().toArray();System.out.println("鍒犻櫎鐩稿悓鍚庣殑鏁扮粍涓:"+Arrays.toString(a))...
绛旓細public static void main(String[] args) { int[] ary = {2,5,4,7,8,9,6,4};//閫氳繃Set闆嗗悎鐨勫敮涓鎬ф潵鍋氬埌鍘婚櫎閲嶅 Set<Integer> set = new TreeSet<Integer>();//鍒╃敤map鐨凨EY鐨勫敮涓鎬ф潵鍘婚櫎閲嶅 Map<Integer, Object> map = new HashMap<Integer, Object>();//鍒╃敤闆嗗悎鐗规у幓闄...
绛旓細java鏁扮粍涓幓鎺閲嶅鏁版嵁鍙互浣跨敤set闆嗗悎锛宻et鏈韩鍐呴儴鏄笉淇濆瓨閲嶅鐨勬暟鎹殑锛屽涓嬩唬鐮侊細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...
绛旓細public class ClearUp { / 鍐掓场鎺掑簭鐨勪竴鐐逛笢涓滐紝浣犲彲浠ュ啀浼樺寲鐨勩/ public static void sorting(int[] buffered){ int temp = 0;for (int i = 0; i < buffered.length - 1; i++) { for (int n = i + 1; n < buffered.length; n++) { if (buffered[n] < buffered[i]) { t...
绛旓細鍏跺疄鍙閬嶅巻鏁扮粍锛岀劧鍚庢斁杩泂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+ " ");} ...
绛旓細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....