博客
关于我
刷题:判断是否存在重复元素
阅读量: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/

    你可能感兴趣的文章
    mysql 存储过程每隔一段时间执行一次
    查看>>
    mysql 存在update不存在insert
    查看>>
    Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
    查看>>
    Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
    查看>>
    Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
    查看>>
    Mysql 学习总结(89)—— Mysql 库表容量统计
    查看>>
    mysql 实现主从复制/主从同步
    查看>>
    mysql 审核_审核MySQL数据库上的登录
    查看>>
    mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
    查看>>
    mysql 导入导出大文件
    查看>>
    mysql 将null转代为0
    查看>>
    mysql 常用
    查看>>
    MySQL 常用列类型
    查看>>
    mysql 常用命令
    查看>>
    Mysql 常见ALTER TABLE操作
    查看>>
    MySQL 常见的 9 种优化方法
    查看>>
    MySQL 常见的开放性问题
    查看>>
    Mysql 常见错误
    查看>>
    MYSQL 幻读(Phantom Problem)不可重复读
    查看>>
    mysql 往字段后面加字符串
    查看>>