We use quantum walks to construct a new quantum algorithm for element distinctness and its generalization. For element distinctness (the problem of finding two equal items among N given items), we get an 0(n^{{2 \mathord{\left/ {\vphantom {2 3}} \right. \kern-\nulldelimiterspace} 3}}) query quantum algorithm. This improves the previous 0(n^{{3 \mathord{\left/ {\vphantom {3 4}} \right. \kern-\nulldelimiterspace} 4}} ) quantum algorithm of Buhrman et al. [10] and matches the lower bound by Shi [28]. We also give an 0(N^{{k \mathord{\left/ {\vphantom {k {(k + 1)}}} \right. \kern-\nulldelimiterspace} {(k + 1)}}} ) query quantum algorithm for the generalization of element distinctness in which we have to find k equal items among N items.