weixin_39121325
疯子!!!
采纳率0%
2017-10-15 02:27 阅读 915

java编写一个:去掉数组或集合中重复次数最少的数据

去掉数组或集合中重复次数最少的数据
例如:
输入:1 1 1 2 4 4 6 7
输出:2 6 7

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • zhedanxc 哲淡 2017-10-15 05:28
    package hello;
    
    import java.util.HashMap;
    import java.util.Collections;
    
    public class Demo{
        public static void main(String[] argv) {
            int[] nums = {1, 1, 1, 2, 4, 4, 6, 7};
            Counter counter = new Counter();
            for(int i : nums) {
                counter.put(i);
            }
    
            int min_count = (int)Collections.min(counter.values());
            Object[] keys = counter.keySet().toArray();
            for(Object i: keys) {
                int value_count = counter.get(i);
                if (value_count != min_count) {
                    counter.remove(i);
                }
            }
    
            for(int i: counter.keySet()) {
                System.out.println(i);
            }
        }
    }
    
    
    class Counter extends HashMap<Integer, Integer>{
        private static final long serialVersionUID = 1L;
    
        public void put(int value) {
            int count = 0;
            if (this.containsKey(value)) {
                count = this.get(value);
                count += 1;
                this.put(value, count);
            }else {
                count = 1;
                this.put(value, count);
            }
        }
    }
    
    点赞 1 评论 复制链接分享
  • Small_Mouse0 鼠小 2017-10-15 04:06

    http://blog.csdn.net/small_mouse0/article/details/77543447

    提供了三种方法,,题主看到懂了,改一改就ok了

    点赞 评论 复制链接分享
  • weixin_38213019 Ccaveman 2017-10-15 04:17

    List list = new ArrayList();
    list.add(1);
    list.add(1);
    list.add(2);
    list.add(2);
    list.add(4);
    list.add(4);
    list.add(6);
    list.add(7);
    System.out.println(list.toString());
    Set sets = new HashSet(list);
    System.out.println(sets.toString());

    点赞 评论 复制链接分享
  • u012633496 BigBearSky 2017-10-15 05:34

    public class Test {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("请您输入字符串:");
    String str = scanner.nextLine();
    String result = getLeastRepeat(str);
    System.out.println("重复最少的数据是:" + result);
    }

    public static String getLeastRepeat(String str) {
        String result = "";
        if (str == null || str.trim().equals("")) {
            return result;
        }
        String replaceStr = str.trim().replace(" ", ";");
        String[] strArr = replaceStr.split(";");
        if (strArr.length == 1) {
            return strArr[0];
        }
        Map<String, Integer> map = new HashMap<>();
        for (String example : strArr) {
            if (example == null || example.equals("")) {
                continue;
            }
            Integer count = map.get(example);
            if (count == null) {
                map.put(example, 1);
            } else {
                map.put(example, count + 1);
            }
        }
        System.out.println(JSON.toJSONString(map));
        if (map.size() == 0) {
            return result;
        } else if (map.size() == 1){
            return map.keySet().toString();
        }
        Integer leastCount = 0;
        for (Integer count : map.values()) {
            if (leastCount == 0) {
                leastCount = count;
                continue;
            }
            if (count < leastCount) {
                leastCount = count;
            }
        }
        for(Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == leastCount) {
                result += entry.getKey() + " ";
            }
        }
        if (!result.equals("")) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }
    

    }

    点赞 评论 复制链接分享

相关推荐