to find repetative character in string
How can I find distin开发者_StackOverflow中文版ct repetitive character in string using Java.
For the string 4567895443577
Here, the first distinct repetitive character is 5
Ip:n:1 output:4
n=2 op=5
n=3 op=7
n=4 op=doest exist
create HashSet
and HashMap
: set,map and int
count=0,
iterate over the string, and add each character and its index. at the end - each character's value will be the LAST index.
iterate over the String again, and check if the index is as appears in the map. if it does (or the character appears in the set) - ignore it.
if a character is not in the set, and index as is and as in map don't match - increase count (until it reaches n).
complexity: O(n)
public static Character findN(String str,int n) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int len = str.length();
for (int i=0;i<len;i++) {
map.put(str.charAt(i),i);
}
int count=0;
HashSet<Character> set = new HashSet<Character>();
for (int i=0;i<len;i++) {
if (set.contains(str.charAt(i))) continue;
if (map.get(str.charAt(i)) != i) {
count++;
if (count == n) return str.charAt(i);
set.add(str.charAt(i));
}
}
return null; //it does not exist
}
This should work:
public static char findChar(String s, int length) {
int[] counts = new int[10];
// iterate over the letters and increment the count
int stringLength = s.length();
for(int i = 0; i < stringLength; i++ ) {
char c = s.charAt(i);
int value = Character.getNumericValue(c);
counts[value]++;
}
int counter = 0; // how many chars repeated so far
for(int i = 0; i < stringLength; i++ ) {
char c = s.charAt(i);
int value = Character.getNumericValue(c);
if(counts[value] >= 2) {
counts[value] = -1; // do not count this twice
counter++;
if(counter == length) {
return c;
}
}
}
return '\u0000'; // null char
}
This can be done by the following code.
I have used HashMap keys as an input characters and value as a counter.
String str = "4567895443577";
char[] chars = str.toCharArray();
HashMap<Character, Integer> charMap = new HashMap<Character, Integer>();
for( char c : chars )
{
if( charMap.containsKey( c ) ){
charMap.put(c, charMap.get(c) + 1 );
}else{
charMap.put(c, 1);
}
}
for( Entry<Character, Integer> entry : charMap.entrySet() )
{
System.out.println( "Character '"+entry.getKey()+"' is repeated for '"+entry.getValue()+"' times." );
}
You should create a HashSet
which implements Set
Interface.
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
/*
* Ex-OR basic : 0^0 = 0, 0^1 = 1, 1^0 = 1, 1^1 = 0
*
Ex-ORing bits of all characters in String nums = "4567895443577"
i Operation Bitwise operation Result(bin) Result(Dec)
0 4 ^ 5 ...arr[0]^arr[1] 100 ^ 101 001 1
//NOTE : first occurence as result = 1 should be skipped
----------------------------------------------------------------------------
Result(i-1) arr[i]
for:
1 1 ^ 5 001 ^ 101 100 4
2 4 ^ 6 100 ^ 110 010 2
3 2 ^ 7 010 ^ 111 101 5
4 5 ^ 8 0101 ^ 1000 1101 13
5 13 ^ 9 1101 ^ 1001 0100 4
6 5 ^ 4 0101 ^ 0100 0001 1
// break "for" found repeated element. return 5
* */
public class RepeatedNumber {
public static void main(String args[]) {
String nums = "4567895443577";
char repeated = (char) findRepeated(nums.toCharArray()) ;
System.out.println("result ="+repeated);
}
public static int findRepeated(char arr[]) {
int result = arr[0]^arr[1];
int repeated = arr[0];
//find out number repeated more than once in array
if(result != 0) {
for(int i = 1; i < arr.length; i++) {
result = result ^ arr[i];
if(result == 1 || arr[i] == arr[i-1]) {
repeated = arr[i];
break;
}
}
}
return repeated;
}
}
精彩评论