【Java】寻找数组中“主要元素”

寻找数组中主要元素

 

题目描述

数组中占比超过一半的元素称为“主要元素”,也就是说,数组某个元素出现的次数大于数组长度的一半,该元素可称为主要元素。给定一个整型数组,找到它的主要元素,如果没有则返回-1。

解决思路

双指针法
画图示意一下在这里插入图片描述

实现代码

<span style="color:#000000"><code class="language-java"><span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">int</span> <span style="color:#3d8fd1">majorElement</span><span style="color:#999999">(</span><span style="color:#6679cc">int</span><span style="color:#999999">[</span><span style="color:#999999">]</span> arr<span style="color:#999999">)</span><span style="color:#999999">{</span>

<span style="color:#6b7394">//对于此题而言,把握好i和j的边界非常关键</span>
        <span style="color:#6679cc">for</span> <span style="color:#999999">(</span><span style="color:#6679cc">int</span> i <span style="color:#ac9739">=</span> <span style="color:#ac9739">0</span><span style="color:#999999">;</span> i <span style="color:#ac9739"><=</span> arr<span style="color:#999999">.</span>length<span style="color:#ac9739">/</span><span style="color:#ac9739">2</span><span style="color:#999999">;</span> i<span style="color:#ac9739">++</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>

            <span style="color:#6679cc">for</span> <span style="color:#999999">(</span><span style="color:#6679cc">int</span> j <span style="color:#ac9739">=</span> arr<span style="color:#999999">.</span>length<span style="color:#ac9739">/</span><span style="color:#ac9739">2</span><span style="color:#999999">;</span> j <span style="color:#ac9739"><</span> arr<span style="color:#999999">.</span>length<span style="color:#999999">;</span> j<span style="color:#ac9739">++</span><span style="color:#999999">)</span> <span style="color:#999999">{</span>

                <span style="color:#6679cc">if</span><span style="color:#999999">(</span>arr<span style="color:#999999">[</span>i<span style="color:#999999">]</span> <span style="color:#ac9739">==</span> arr<span style="color:#999999">[</span>j<span style="color:#999999">]</span><span style="color:#999999">)</span><span style="color:#999999">{</span>
                    <span style="color:#6679cc">return</span> arr<span style="color:#999999">[</span>i<span style="color:#999999">]</span><span style="color:#999999">;</span>
                <span style="color:#999999">}</span>

            <span style="color:#999999">}</span>

        <span style="color:#999999">}</span>
        <span style="color:#6679cc">return</span> <span style="color:#ac9739">-</span><span style="color:#ac9739">1</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>

    <span style="color:#6679cc">public</span> <span style="color:#6679cc">static</span> <span style="color:#6679cc">void</span> <span style="color:#3d8fd1">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span>

        <span style="color:#6679cc">int</span><span style="color:#999999">[</span><span style="color:#999999">]</span> arr1 <span style="color:#ac9739">=</span> <span style="color:#999999">{</span><span style="color:#ac9739">4</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">8</span><span style="color:#999999">,</span><span style="color:#ac9739">8</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">8</span><span style="color:#999999">,</span><span style="color:#ac9739">10</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">,</span><span style="color:#ac9739">3</span><span style="color:#999999">}</span><span style="color:#999999">;</span>
        System<span style="color:#999999">.</span>out<span style="color:#999999">.</span><span style="color:#3d8fd1">println</span><span style="color:#999999">(</span><span style="color:#ac9739">"数组中的主要元素为: "</span><span style="color:#ac9739">+</span><span style="color:#3d8fd1">majorElement</span><span style="color:#999999">(</span>arr1<span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
    <span style="color:#999999">}</span>
<span style="color:#999999">}</span>
</code></span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

运行结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值