Interview Question
Developer Program EngineersTeam: Dev
Country: India
Interview Type: In-Person
Generally with some form of hash structure.
If you can't have another data structure, I guess it depends on the input data and the definition of 'scanned'. If faster than O(N) does not count as 'scanned' then you could insert the elements in sorted order and then discover whether an element is duplicate in O(log(N)).
If the input data is of a limited range (say integers from 0-100) then you could encode each value as a bit in a sequence of bytes and switch it to 1 when the element is entered, and then check if the bit is 1 whenever a new element is entered, rejecting the entry if the bit is already 1.
package com.kavitha.selfhelp;
import java.util.HashSet;
import java.util.Set;
public class StoreSet {
public static void main (String[] args){
Set <Integer> sampleset = new HashSet<Integer>();
System.out.println("No repetition see below: ");
for (String s : args){
Integer input = Integer.parseInt(s);
sampleset.add(input);
}
for(Integer setint : sampleset){
System.out.println( setint );
}
}
}
Keep storing in the HashMap..?
- naveenhooda2004 March 23, 2013