博客
关于我
刷题:判断是否存在重复元素
阅读量: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 join原理
    查看>>
    MySQL Join算法与调优白皮书(二)
    查看>>
    Mysql order by与limit混用陷阱
    查看>>
    Mysql order by与limit混用陷阱
    查看>>
    mysql order by多个字段排序
    查看>>
    MySQL Order By实现原理分析和Filesort优化
    查看>>
    mysql problems
    查看>>
    mysql replace first,MySQL中处理各种重复的一些方法
    查看>>
    MySQL replace函数替换字符串语句的用法(mysql字符串替换)
    查看>>
    mysql replace用法
    查看>>
    Mysql Row_Format 参数讲解
    查看>>
    mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
    查看>>
    MySQL Server 5.5安装记录
    查看>>
    mysql server has gone away
    查看>>
    mysql slave 停了_slave 停止。求解决方法
    查看>>
    MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
    查看>>
    MYSQL sql语句针对数据记录时间范围查询的效率对比
    查看>>
    mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
    查看>>
    mysql Timestamp时间隔了8小时
    查看>>
    Mysql tinyint(1)与tinyint(4)的区别
    查看>>