博客
关于我
刷题:判断是否存在重复元素
阅读量: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主从不同步
    查看>>
    mysql主从同步及清除信息
    查看>>
    MySQL主从同步相关-主从多久的延迟?
    查看>>
    mysql主从同步配置方法和原理
    查看>>
    mysql主从复制 master和slave配置的参数大全
    查看>>
    MySQL主从复制几个重要的启动选项
    查看>>
    MySQL主从复制及排错
    查看>>
    mysql主从复制及故障修复
    查看>>
    MySQL主从复制的原理和实践操作
    查看>>
    webpack loader配置全流程详解
    查看>>
    mysql主从复制,读写分离,半同步复制实现
    查看>>
    MySQL主从失败 错误Got fatal error 1236解决方法
    查看>>
    MySQL主从架构与读写分离实战
    查看>>
    MySQL主从篇:死磕主从复制中数据同步原理与优化
    查看>>
    mysql主从配置
    查看>>
    MySQL之2003-Can‘t connect to MySQL server on ‘localhost‘(10038)的解决办法
    查看>>
    MySQL之CRUD
    查看>>
    MySQL之DML
    查看>>
    Mysql之IN 和 Exists 用法
    查看>>