如何接收一个字符数组,并在同一个字符出现多次时返回true。

huangapple go评论59阅读模式
英文:

How to take an array of chars and return true if the same character appears more than once

问题

例如:containsTwo( ['a', 'b', 'c'] ) --> false.
containsTwo( ['c', 'a', 'c'] ) --> true

public static boolean containsTwo(char[] arr) {
  int pos = 0;
  for (int i = 0; i < arr.length; i++) {
    for (int i2 = 1; i2 < arr.length -1;i2++) {
    if (arr[pos] == arr[i]) {
    return true;
    }
    pos++;
  } return false;
}

我在弄清楚如何遍历数组以检查 arr[0] 是否与 arr[2] 或 arr[3] 相同方面遇到了问题。

英文:

For example: containsTwo( ['a', 'b', 'c'] ) --> false.
containsTwo( ['c', 'a', 'c'] ) --> true

public static boolean containsTwo(char[] arr) {
  int pos = 0;
  for (int i = 0; i &lt; arr.length; i++) {
    for (int i2 = 1; i2 &lt; arr.length -1;i2++) {
    if (arr[pos] == arr[i]) {
    return true;
    }
    pos++;
  } return false;
}

I am having trouble figuring out how I would traverse through the array to check if arr[0] is the same as arr[2] or arr[3]

答案1

得分: 1

用 Set 来替代双重循环。

public static boolean containsTwo(char[] arr) {
    Set<Character> set = new HashSet<>();
    for (char a : arr) {
        if (set.add(a) == false) {
            return true;
        }
    }
    return false;
}
英文:

Instead of using double for loops, consider using Set.

public static boolean containsTwo(char[] arr) {
    Set&lt;Character&gt; set = new HashSet&lt;&gt;();
    for (char a : arr) {
        if (set.add(a) == false) {
            return true;
        }
    }
    return false;
}

答案2

得分: 0

你正在正确的轨道上。你从左到右查看。如果当前位置右边的任何字符与当前位置相匹配,则返回 true。如果检查所有字符后仍没有匹配,则返回 false。像这样,

public static boolean containsTwo(char[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[i] == arr[j]) {
                return true;
            }
         }
     }
     return false;
}
英文:

You're on the right track. You look from left to right. If any character to the right of the present position matches the present position return true. If you examine all of the characters return false. Like,

public static boolean containsTwo(char[] arr) {
    for (int i = 0; i &lt; arr.length - 1; i++) {
        for (int j = i + 1; j &lt; arr.length; j++) {
            if (arr[i] == arr[j]) {
                return true;
            }
         }
     }
     return false;
}

答案3

得分: 0

Comparing each element with all other elements of char[] will become more complex in case of a huge number of elements.
If your requirement is only to return True or False depending on whether any duplicate character exists in char[], then kindly try to add char[] elements to a Set, because Set will only accept unique values. So, when you are adding an element that is already present in the Set, it will return False and fulfill your requirement.
Kindly use the complete code below.

import java.util.HashSet;
import java.util.Set;

public class ArrayToSet {

    public static void main(String[] args) {
        ArrayToSet arraySet = new ArrayToSet();
        char[] chars = { 'a', 'b', 'c' };
        System.out.println(arraySet.containsTwo(chars));
    }

    public static boolean containsTwo(char[] arr) {
        Set<Character> charSet = new HashSet<>();
        for (char ch : arr) {
            if (charSet.add(ch) == false) {
                return true;
            }
        }
        return false;
    }
}
英文:

Comparing each element with all other elements of char[] will become more complex in case of huge number of elements.<br>
If your reuirement is only to return True or False depending upon any duplicate character exist in char[], then kindly try to add char[] elements to Set because Set will accept only unique values so; when you are adding element which is already present in Set then it will return False and your requirement fulfills.<br>
Kindly use below complete code.<br>

import java.util.HashSet;
import java.util.Set;

public class ArrayToSet {

	public static void main(String[] args) {
		ArrayToSet arraySet = new ArrayToSet();
		char[] chars = { &#39;a&#39;, &#39;b&#39;, &#39;c&#39; };
		System.out.println(arraySet.containsTwo(chars));
	}

	public static boolean containsTwo(char[] arr) {
		Set&lt;Character&gt; charSet = new HashSet&lt;&gt;();
	    for (char ch : arr) {
	        if (charSet.add(ch) == false) {
	            return true;
	        }
	    }
	    return false;
	}
} 

huangapple
  • 本文由 发表于 2020年10月11日 07:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64299255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定