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

    你可能感兴趣的文章
    mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
    查看>>
    mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
    查看>>
    mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
    查看>>
    mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
    查看>>
    MySQL 8.0 恢复孤立文件每表ibd文件
    查看>>
    MySQL 8.0开始Group by不再排序
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    multi swiper bug solution
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    MySQL binlog三种模式
    查看>>
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    MySQL Cluster与MGR集群实战
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>