当前访客身份:游客 [ 登录 | 加入 OSCHINA ]

代码分享

当前位置:
代码分享 » Java  » 编程基础
鉴客

Java 删除数组中的重复元素

鉴客 发布于 2014年12月04日 7时, 7评/3331阅
分享到: 
收藏 +0
2
http://javarevisited.blogspot.jp/2014/01/how-to-remove-duplicates-from-array-java-without-collection-API.html
标签: <无>

代码片段(2) [全屏查看所有代码]

1. [代码]TechnicalInterviewTest.java     跳至 [1] [2] [全屏预览]

import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Java program to remove duplicates from this array. You don't
 * need to physically delete duplicate elements, replacing with null, or
 * empty or default value is ok.
 *
 * @author http://javarevisited.blogspot.com
 */
public class TechnicalInterviewTest {

    private static final Logger logger = LoggerFactory.getLogger(TechnicalInterviewTest.class);

    public static void main(String args[]) {

        int[][] test = new int[][]{
            {1, 1, 2, 2, 3, 4, 5},
            {1, 1, 1, 1, 1, 1, 1},
            {1, 2, 3, 4, 5, 6, 7},
            {1, 2, 1, 1, 1, 1, 1},};

        for (int[] input : test) {
            System.out.println("Array with Duplicates       : " + Arrays.toString(input));
            System.out.println("After removing duplicates   : " + Arrays.toString(removeDuplicates(input)));
        }
    }

    /*
     * Method to remove duplicates from array in Java, without using
     * Collection classes e.g. Set or ArrayList. Algorithm for this
     * method is simple, it first sort the array and then compare adjacent
     * objects, leaving out duplicates, which is already in result.
     */
    public static int[] removeDuplicates(int[] numbersWithDuplicates) {

        // Sorting array to bring duplicates together      
        Arrays.sort(numbersWithDuplicates);     
      
        int[] result = new int[numbersWithDuplicates.length];
        int previous = numbersWithDuplicates[0];
        result[0] = previous;

        for (int i = 1; i < numbersWithDuplicates.length; i++) {
            int ch = numbersWithDuplicates[i];

            if (previous != ch) {
                result[i] = ch;
            }
            previous = ch;
        }
        return result;

    }
}

2. [代码]运行结果     跳至 [1] [2] [全屏预览]

 Output :
Array with Duplicates       : [1, 1, 2, 2, 3, 4, 5]
After removing duplicates   : [1, 0, 2, 0, 3, 4, 5]
Array with Duplicates       : [1, 1, 1, 1, 1, 1, 1]
After removing duplicates   : [1, 0, 0, 0, 0, 0, 0]
Array with Duplicates       : [1, 2, 3, 4, 5, 6, 7]
After removing duplicates   : [1, 2, 3, 4, 5, 6, 7]
Array with Duplicates       : [1, 2, 1, 1, 1, 1, 1]
After removing duplicates   : [1, 0, 0, 0, 0, 0, 2]


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(7)

  • 1楼:优雅先生 发表于 2014-12-09 01:43 回复此评论
    既然标题是“  how-to-remove-duplicates-from-array-java-without-collection-API”,但代码中却出现了Arrays.sort(numbersWithDuplicates);,Arrays也算是集合类中常用的工具类。其次删除重复元素,数组应该收缩,而不是把重复的元素设置为0。仅为个人观点。
  • 2楼:JR1970 发表于 2014-12-11 10:28 回复此评论
    拿出来放到set中,然后再拿出来,重复的不就没了?
  • 3楼:库特 发表于 2014-12-11 11:55 回复此评论
    标题党
  • 4楼:xuegao521 发表于 2014-12-11 12:05 回复此评论
    标题党
  • 5楼:ixiaohei 发表于 2014-12-11 16:41 回复此评论
    标题党
  • 6楼:帰郷 发表于 2014-12-11 18:56 回复此评论
    没有实现吧。好多BUG
  • 7楼:二龙湖小峰哥 发表于 2014-12-18 16:14 回复此评论
    支持2楼
    public  static Object[] removeDuplicates3(int[]a){
      Set<Integer> s=new HashSet<Integer>();
      for(int i=0;i<a.length;i++){
      s.add(a[i]);
      }
      return s.toArray();
       }
开源从代码分享开始 分享代码