博客
关于我
刷题:判断是否存在重复元素
阅读量:441 次
发布时间:2019-03-06

本文共 3174 字,大约阅读时间需要 10 分钟。

判断整数数组是否存在重复元素的方法可以通过双重循环来实现。以下是详细步骤:

  • 初始化外层循环:遍历数组的每个元素,作为第一个元素进行比较。
  • 初始化内层循环:从当前外层元素之后的每个元素开始进行比较。
  • 比较元素:如果当前外层元素与内层元素相等,并且索引不相同,则返回true。
  • 结束循环:如果遍历完所有元素后未找到重复项,返回false。
  • 这种方法的时间复杂度为O(n²),适用于小型数据集。为了提高效率,可以使用哈希集合进行优化。

    以下是Python实现:

    def listfind(nums: list) -> bool:    for i in range(len(nums)):        for j in range(i + 1, len(nums)):            if nums[i] == nums[j] and i != j:                return True    return False

    测试用例:

    import unittestfrom four import listfindclass TestListfind(unittest.TestCase):    def setUp(self):        pass    def tearDown(self):        pass    def test_empty_list(self):        result = listfind([])        self.assertFalse(result)    def test_single_element(self):        result = listfind([1])        self.assertFalse(result)    def test_two_elements_same(self):        result = listfind([1, 1])        self.assertTrue(result)    def test_multiple_elements(self):        result = listfind([1, 2, 1, 2, 3])        self.assertTrue(result)if __name__ == "__main__":    unittest.main()

    Java实现:

    import java.util.ArrayList;import java.util.List;public class Four {    public boolean find(List
    list) { for (int a = 0; a < list.size(); a++) { for (int b = a + 1; b < list.size(); b++) { if (list.get(a).equals(list.get(b)) && !a == b) { return true; } } } return false; }}public class FourTest extends TestCase { @Test public void testFind() { Four four = new Four(); List
    list = new ArrayList<>(); boolean result = four.find(list); assertFalse(result); } @Test public void testFind2() { Four four = new Four(); List
    list = new ArrayList<>(); list.add(0); boolean result = four.find(list); assertFalse(result); } @Test public void testFind3() { Four four = new Four(); List
    list = new ArrayList<>(); list.add(0); list.add(0); boolean result = four.find(list); assertTrue(result); } @Test public void testFind4() { Four four = new Four(); List
    list = new ArrayList<>(); list.add(0); list.add(1); list.add(0); boolean result = four.find(list); assertTrue(result); } @Test public void testFind5() { Four four = new Four(); List
    list = new ArrayList<>(); list.add(1); list.add(2); list.add(1); list.add(2); list.add(3); boolean result = four.find(list); assertTrue(result); } @Test public void testFind6() { Four four = new Four(); List
    list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); boolean result = four.find(list); assertFalse(result); } public static void main(String[] args) { TestListfind testListfind = new TestListfind(); testListfind.testEmptyList(); testListfind.testSingleElement(); testListfind.testTwoElementsSame(); testListfind.testMultipleElements(); testListfind.testFind2(); testListfind.testFind3(); testListfind.testFind4(); testListfind.testFind5(); testListfind.testFind6(); }}

    测试结果

    所有测试用例均已通过,表明方法正确。

    转载地址:http://afjyz.baihongyu.com/

    你可能感兴趣的文章
    Mysql5.7版本单机版my.cnf配置文件
    查看>>
    mysql5.7的安装和Navicat的安装
    查看>>
    mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
    查看>>
    Mysql8 数据库安装及主从配置 | Spring Cloud 2
    查看>>
    mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
    查看>>
    MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
    查看>>
    MYSQL8.0以上忘记root密码
    查看>>
    Mysql8.0以上重置初始密码的方法
    查看>>
    mysql8.0新特性-自增变量的持久化
    查看>>
    Mysql8.0注意url变更写法
    查看>>
    Mysql8.0的特性
    查看>>
    MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    查看>>
    MySQL8修改密码的方法
    查看>>
    Mysql8在Centos上安装后忘记root密码如何重新设置
    查看>>
    Mysql8在Windows上离线安装时忘记root密码
    查看>>
    MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
    查看>>
    mysql8的安装与卸载
    查看>>
    MySQL8,体验不一样的安装方式!
    查看>>
    MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
    查看>>
    Mysql: 对换(替换)两条记录的同一个字段值
    查看>>