Java在整数数组中重复 [英] Java duplicates in integer array

查看:137
本文介绍了Java在整数数组中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要查找 int [] 数组是否包含重复项。我不能使用收藏品。

I need to find if an int[] array contains duplicates. I can not use collections.

我的解决方案(不起作用)是:

My solution (which does not work) is:

boolean containsDuplicates(int[]list1, int[]list2) {
  if (list1.length != list2.length) {
    return false;
  }
  for (int i = 0; i < list1.length; i++) {
    if (list1[i] != list2[i]) {
      return false;
    }
  }
  return true;
}


推荐答案

解决方案,不是最好的在计算周期但没有内存开销:

A solution, not the best in computation cycles but without memory overhead:


  1. 一个循环,用于将数组A从0迭代到N,索引为M

  2. 一个嵌套循环,用于将A从M + 1迭代到N,索引为S

  3. if(A [M] = A [S])返回true

  4. 返回false(未找到重复项)

  1. A loop to iterate the array A from 0 to N with index M
  2. A nested loop to iterate A from M+1 to N with index S
  3. if( A[M]=A[S] ) return true
  4. return false (no duplicates found)

这篇关于Java在整数数组中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆